Type difference of character literals in C and C++
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++的更多相关文章
- [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++中有不同的变量 ...
- Oracle Schema Objects——Tables——Oracle Data Types
Oracle Schema Objects Oracle Data Types 数据类型 Data Type Description NUMBER(P,S) Number value having a ...
- Library string Type
The string type supports variable-length character strings.The library takes cares of managing memor ...
- Type system
Type system[edit] Main articles: Data type, Type system, and Type safety A type system defines how a ...
- 【leetcode】1218. Longest Arithmetic Subsequence of Given Difference
题目如下: Given an integer array arr and an integer difference, return the length of the longest subsequ ...
- (转) Pointers
原地址 http://www.cplusplus.com/doc/tutorial/pointers/ Pointers In earlier chapters, variables have bee ...
- 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 ...
- modern-cpp-features
C++17/14/11 Overview Many of these descriptions and examples come from various resources (see Acknow ...
- 关于C#你应该知道的2000件事
原文 关于C#你应该知道的2000件事 下面列出了迄今为止你应该了解的关于C#博客的2000件事的所有帖子. 帖子总数= 1,219 大会 #11 -检查IL使用程序Ildasm.exe d #179 ...
随机推荐
- 小米多模网关接入Home Assistant ZNDMWG03LM
一.小米zigbee网关使用 先下载米家app,打开手机蓝牙,登陆点"我的"界面,将网关设备插上电源,橙灯闪烁,点击蓝牙网关等待弹窗提示连接,选择连接路由器(需2.4GHz),输入 ...
- robot framework error: [ ERROR ] Suite 'XXX' contains no tests or tasks.(解决方法)
robot framework 按照如下操作创建项目 一.创建项目 选择菜单栏file----->new Project Name 输入项目名称. Type 选择Directory. 二.创建测 ...
- k8s endpoints controller分析
k8s endpoints controller分析 endpoints controller简介 endpoints controller是kube-controller-manager组件中众多控 ...
- inline-block布局VS浮动布局
a.不同之处:对元素设置display:inline-block ,元素不会脱离文本流,而float就会使得元素脱离文本流,且还有父元素高度坍塌的效果 b.相同之处:能在某程度上达到一 ...
- Effective C++ 总结笔记(一)
一.让自己习惯C++ 01.视C++为一个语言联邦 c++是多重范型编程 语言,视c++包括4种次语言: 1:C 2:Object-Oreinted C++: 3:Template C++: 4:ST ...
- 大爽Python入门教程 3-2 条件判断: if...elif..else
大爽Python入门公开课教案 点击查看教程总目录 简单回顾if 回顾下第一章的代码 >>> x = 5 >>> if x > 0: ... print(&q ...
- Python如何格式化输出
目录 Python中的格式化输出 1.旧格式化 2.新格式format( ) 函数 Python中的格式化输出 格式化输出就是将字符串中的某些内容替换掉再输出就是格式化输出 旧格式化输出常用的有%d( ...
- 菜鸡的Java笔记 第十 - java 类与对象
(局部变量需要初始化,全局变量不初始化系统也会帮忙初始化而局部变量系统不会帮忙初始化)>>> 2.1 类与对象基本概念 在现实生活之中,类指的就是具备某一共性的群 ...
- centos7.6自动化安装mysql5.5
一.目的 简化安装mysql的安装过程,局限很大,仅支持centos7.6上安装mysql5.5.60,如果想在其他版本的操作系统安装mysql,请自行修改有关变量. 如果想了解mysql安装的具体过 ...
- 微软商店打不开的教程(错误代码0x80131500)
1 打开win+R 输入`inetcpl.cpl` 2 点击高级 3 勾选上`使用TLS 1.2`或者点击还原默认设置就可以啦 4 然后就可以打开啦