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. redis开外网访问

    Redis: 注释掉bind 127.0.0.1可以使所有的ip访问redis 若是想指定多个ip访问,但并不是全部的ip访问,可以bind protected-mode no /etc/init.d ...

  2. 【微服务落地】服务间通信方式: gRPC的入门

    gRPC是什么 官方介绍: https://grpc.io/docs/what-is-grpc/introduction/ "A high-performance, open-source ...

  3. 刷题日记-JZ25合并有序链表

    合并有序链表 递归方式合并链表pHead1,pHead2 base case是 pHead1为空或者pHead2为空 递归方式是 如果pHead1->val < pHead2->va ...

  4. mybatis之参数传递的方式 | mybatis

    1.单个参数(基本类/包装类+String) 这种情况MyBatis可直接使用这个参数,不需要经过任何处理. 一个参数情况下#{}中内容随便写 public Employee getEmployeeB ...

  5. [LINUX] Arch Linux 硬盘拷贝式装系统+新增 home 分区

    目录 前言 1. 实操 1.1 整个磁盘拷贝 1.2 创建 home 分区 1.3 修改 fstab 实现自动挂载 2. 涉及到的知识点 2.1 fstab 2.2 dd 命令 2.3 fdisk 命 ...

  6. 手撸一个IOC容器

    IoC 什么是IoC? IoC是Inversion of Control(控制反转)的简称,注意它是一个技术思想.描述的是对象创建.管理的事情. 传统开发方式:比如类A依赖类B,往往会在类A里面new ...

  7. install virtualenv without sudo

    用普通用户安装virtualenv Perhaps this was valid for older versions of virtualenv. For now, if you want to r ...

  8. 12组-Alpha冲刺-1/6

    一.基本情况 队名:字节不跳动 组长博客:https://www.cnblogs.com/147258369k/p/15526363.html 小组人数:10人 二.冲刺概况汇报 侯钦凯 过去两天完成 ...

  9. SVGO: Node.js 开发的 SVG 矢量图优化工具(svg压缩工具)

    SVG图片压缩 这是个通过借助npm包的一种方式去压缩svg的图片,由于阿里的图库自己创建的图标有大小的限制,当我们想要自己用自己的图标的时候就可以使用这种方式去完成对svg的图片压缩. 1.下载no ...

  10. Spring Cloud Gateway实战之五:内置filter

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...