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. docker 加入域名

    先运行docker镜像 # 进入 docker 容器 mynginx 是容器名 docker exec -i -t mynginx /bin/bash #安装vim apt-get install v ...

  2. 第40篇-JNIEnv和JavaVM

    下面介绍2个与JNI机制相关的类型JNIEnv和JavaVM. 1.JNIEnv JNIEnv一般是是由虚拟机传入,而且与线程相关的变量,也就说线程A不能使用线程B的JNIEnv.而作为一个结构体,它 ...

  3. Oracle system 用户无法登录问题

    新手刚用Oracle数据库时,可能会遇到system用户无法登录情况. 问题原因:1.可能输入默认密码时输入错误(比较低级,一般不会范). 2.可能你在安装的时候设置了密码,但是在登录的时候密码不正确 ...

  4. 从零开始制作一个linux iso镜像

    一.前言     对于一个极简化的linux系统而言,只需要三个部分就能组成,它们分别是一个linux内核.一个根文件系统和引导.以下是本文制作linux iso镜像所用到的系统和软件:     OS ...

  5. java 模版式的 word

    ... package com.kingzheng.projects.word; import java.io.BufferedWriter; import java.io.File; import ...

  6. Python 函数 参数传递

    参数传递    在 python 中,类型属于对象,变量是没有类型的:        a=[1,2,3]        a="Runoob"    以上代码中,[1,2,3] 是 ...

  7. 什么。你还没有搞懂Spring事务增强器 ,一篇文章让你彻底搞懂Spring事务,虽然很长但是干货满满

    上一篇文章主要讲解了事务的Advisor是如何注册进Spring容器的,也讲解了Spring是如何将有配置事务的类配置上事务的,也讲解了Advisor,pointcut验证流程:但是还未提到的那个Ad ...

  8. 十本你不容错过的Docker入门到精通书籍推荐

    前言: 最近有许多小伙伴私信让我推荐几本关于Docker学习的书籍,今天花了一下午的时间在网上查阅了一些资料和结合自己平时工作中的一些学习参考资料书籍写下了这篇文章.注意以下书籍都是十分优秀的Dock ...

  9. [bzoj1107]驾驶考试

    转化题意,如果一个点k符合条件,当且仅当k能到达1和n考虑如果l和r($l<r$)符合条件,容易证明那么[l,r]的所有点都将会符合条件,因此答案是一个区间枚举答案区间[l,r],考虑如何判定答 ...

  10. Jetpack架构组件学习(2)——ViewModel和Livedata使用

    要看本系列其他文章,可访问此链接Jetpack架构学习 | Stars-One的杂货小窝 原文地址:Jetpack架构组件学习(2)--ViewModel和Livedata使用 | Stars-One ...