首页 > 编程语言 > 详细

ype difference of character literals in C and C++

时间:2015-09-22 18:08:58      阅读:227      评论:0      收藏:0      [点我收藏+]

http://www.geeksforgeeks.org/g-fact-54/

 

Every literal (constant) in C/C++ will have a type information associated with it.

In both C and C++, numeric literals (e.g. 10) will have int as their type. It means sizeof(10) and sizeof(int)will return same value.

However, character literals (e.g. ‘V’) will have different types, sizeof(‘V’) returns different values in C and C++. In C, a character literal is treated as int type where as in C++, a character literal is treated as chartype (sizeof(‘V’) and sizeof(char) are same in C++ but not in C).

int main()
{
   printf("sizeof(‘V‘) = %d sizeof(char) = %d", sizeof(‘V‘), sizeof(char));
   return 0;
}

Result of above program:

C result – sizeof(‘V’) = 4 sizeof(char) = 1

C++ result – sizeof(‘V’) = 1 sizeof(char) = 1

Such behaviour is required in C++ to support function overloading. An example will make it more clear. Predict the output of the following C++ program.

void foo(char c)
{
   printf("From foo: char");
}
void foo(int i)
{
   printf("From foo: int");
}

int main()
{
   foo(‘V‘);
   return 0;
}

The compiler must call

void foo(char);

since ‘V’ type is char.

ype difference of character literals in C and C++

原文:http://www.cnblogs.com/lyleslie/p/4829459.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!