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 char type (sizeof(‘V’) and sizeof(char) are same in C++ but not in C).

1 int main()
2 {
3 printf("sizeof('V') = %d sizeof(char) = %d", sizeof('V'), sizeof(char));
4 return 0;
5 }

  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.

 1 #include <iostream>
2 using namespace std;
3
4 void foo(char c)
5 {
6 printf("From foo: char");
7 }
8 void foo(int i)
9 {
10 printf("From foo: int");
11 }
12
13 int main()
14 {
15 foo('V');
16 return 0;
17 }

  The compiler must call void foo(char);
  since ‘V’ type is char.

  Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

  转载请注明:http://www.cnblogs.com/iloveyouforever/

  2013-11-27  14:50:03

Type difference of character literals in C and C++的更多相关文章

  1. [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++中有不同的变量 ...

  2. Oracle Schema Objects——Tables——Oracle Data Types

    Oracle Schema Objects Oracle Data Types 数据类型 Data Type Description NUMBER(P,S) Number value having a ...

  3. Library string Type

    The string type supports variable-length character strings.The library takes cares of managing memor ...

  4. Type system

    Type system[edit] Main articles: Data type, Type system, and Type safety A type system defines how a ...

  5. 【leetcode】1218. Longest Arithmetic Subsequence of Given Difference

    题目如下: Given an integer array arr and an integer difference, return the length of the longest subsequ ...

  6. (转) Pointers

    原地址 http://www.cplusplus.com/doc/tutorial/pointers/ Pointers In earlier chapters, variables have bee ...

  7. C++ Style Languages: C++, Objective-C, Java, C#

    Hyperpolyglot.org From Hyperpolyglot.org C++ Style Languages: C++, Objective-C, Java, C# a side-by-s ...

  8. modern-cpp-features

    C++17/14/11 Overview Many of these descriptions and examples come from various resources (see Acknow ...

  9. 关于C#你应该知道的2000件事

    原文 关于C#你应该知道的2000件事 下面列出了迄今为止你应该了解的关于C#博客的2000件事的所有帖子. 帖子总数= 1,219 大会 #11 -检查IL使用程序Ildasm.exe d #179 ...

随机推荐

  1. Java基础语法5-运算符

    运算符 基本运算符(算术.赋值.关系.逻辑等)不再赘述 位运算符 位运算符&.|.^.~.<<.>> &与 |或 ~非 ^异或 <<左移 >& ...

  2. 一.Promise入门准备阶段

    一.Promise入门准备阶段 1.区别实例对象呵函数对象 2.两种类型的回调函数(同步与异步) 2.1 同步回调 2.2 异步回调 3.JS的error处理 3.1 错误的类型 3.2 错误处理与错 ...

  3. flask cache

    http://brunorocha.org/python/flask/using-flask-cache.html 如何在大项目中使用cache 新建全局cache.py cache = Cache( ...

  4. ARM汇编解决阶乘以及大小写转换

    环境以及硬件 一.硬件仿真基于 SAMSUNG's S3C44B0X 16/32-bit RISC microprocessor 芯片,仿真器为 J-LINK 二.编写指令软件为 Integrated ...

  5. c++学习笔记3(动态内存分配)

    为了效率,需要按需去进行动态内存分配,在c中,可以用malloc去实现动态内存分配,而在c++中,则用new运算符去实现. 用法一:动态分配一个变量的存储空间 p=new T T为类型名 P则为T*类 ...

  6. 微信小程序(十)

    时间机制- 响应用户交互 时间绑定 冒泡事件与非冒泡事件 bindtap 绑定 VS catchtap 绑定 button 不会阻止bindtap 向上冒泡,而如果是 catchtap 就会阻止向上冒 ...

  7. [cf1458C]Latin Square

    维护$n^{2}$个三元组$(x,y,z)$,每一个三元组描述$a_{x,y}=z$ 对于RLDU这四个操作,即将所有三元组的$x$或$y$执行$\pm 1$(模$n$意义下) 对于IC这两个操作,即 ...

  8. [atARC071F]Infinite Sequence

    注意到当$a_{i}\ne 1$且$a_{i+1}\ne 1$,那么$\forall i<j,a_{j}=a_{i+1}$(证明的话简单归纳就可以了) 令$f_{i}$表示在题中条件下,还满足$ ...

  9. [atARC107F]Sum of Abs

    价值即等价于给每一个点系数$p_{i}=\pm 1$,使得$\forall (x,y)\in E,p_{x}=p_{y}$的最大的$\sum_{i=1}^{n}p_{i}b_{i}$ 如果没有删除(当 ...

  10. 你真的知道Spring注解驱动的前世今生吗?这篇文章让你豁然开朗!

    本篇文章,从Spring1.x到Spring 5.x的迭代中,站在现在的角度去思考Spring注解驱动的发展过程,这将有助于我们更好的理解Spring中的注解设计. Spring Framework ...