ype difference of character literals in C and C++

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.

时间: 2024-10-12 10:24:54

ype difference of character literals in C and C++的相关文章

[C/CPP系列知识] Type difference of character literals 和 bool in C and C++

C/C+中的每一个常亮(every literal)都是有类型的,例如10 就是int型的,因此siziof(10)和sizeof(int)是相同的,但是字符型常亮(‘a’)在C和C++中有不同的变量类型. 在C中,‘a’被认为是int形,在C++中,‘a’被认为是char型. int main() { printf("sizeof('V') = %d sizeof(char) = %d", sizeof('V'), sizeof(char)); return 0; } 结果: C r

Java文法(7)—Literals

----------------------------------------------------------------------------------------------------------------------------------- 说明: A literal is the source code representation of a value of a primitive type (§4.2), the String type (§4.3.3), or th

C++ supports various string and character types,

String and Character Literals (C++) Visual Studio 2015 Other Versions C++ supports various string and character types, and provides ways to express literal values of each of these types. In your source code, you express the content of your character

[C++] String Basic

Namespace Declarations A using declaration let us use a name from a namespace without qualify the name with a namespace_name:: prefix. // using namespace::name using std::vector using std::string  A sperate using declaration is required for each name

Java文法(4)—comments

------------------------------------------------------------------------------------------------------ 说明: There are two kinds of comments. • /* text */ A traditional comment: all the text from the ASCII characters /* to the ASCII characters */ is ig

JAVA面试题最全集

JAVA面试题最全集 2009-01-19 15:40 3458人阅读 评论(0) 收藏 举报 java面试ejbservletstringhashmap 一.Java基础知识1.Java有那些基本数据类型,String是不是基本数据类型,他们有何区别.2.字符串的操作:  写一个方法,实现字符串的反转,如:输入abc,输出cba  写一个方法,实现字符串的替换,如:输入bbbwlirbbb,输出bbbhhtccc.3.数据类型之间的转换  如何将数值型字符转换为数字(Integer,Doubl

SQL Fundamentals:Restricting and Sorting Data限制和排序数据

Restricting and Sorting Data Limit the rows that are retrieved by a query 限制从查询中返回的行 Sort the rows that are retrieved by a query 对查询返回的行进行排序 Use ampersand substitution(&替代变量) to restrict and sort output at runtime 使用替代变量来限制和排序输出 Ampersand 英 [?æmp?sæn

SQL Fundamentals: Basic SELECT statement基本的select语句

Basic SELECT statement基本的select语句 The basic syntax for a SELECT statement is presented below. SELECT语句的基本语法如下. |:多选一 []:可选择的内容 {}:多选一 没有被{}括起来的是必选 SELECT [DISTINCT | ALL] {* | select_list} FROM {table_name [alias] | view_name}     [{table_name [alias

Google Java Style

1 Introduction  This document serves as the complete definition of Google's coding standards for source code in the Java? Programming Language. A Java source file is described as being in Google Style if and only if it adheres to the rules herein. Li