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 ...
随机推荐
- APP自动化之Hybrid自动化解决方案(七)
基于UIAutomator+ChromeDriver模式(UIAutomator安卓原生引擎) 原理:native(原生)部分使用UIAutomator,webview部分使用ChromeDriver ...
- Allure 生成测试报表
Allure官方文档参考地址:https://docs.qameta.io/allure/#_testng 1.在maven中添加依赖并进行相应的配置: <!-- 实现版本控制 --> & ...
- 『学了就忘』Linux基础命令 — 33、管道符
目录 1.管道符介绍 2.管道符应用 (1)例子1: (2)例子2: (3)例子3: 1.管道符介绍 管道符|,也是Shell命令. 管道符的作用是链接多个命令,把命令1的结果作为命令2的操作对象. ...
- Linux mem 2.6 Rmap 内存反向映射机制
文章目录 1. 简介 2. 匿名内存 Rmap 的建立 2.1 fork() 2.2 do_page_fault() 3. 文件内存 Rmap 的建立 3.1 fork() 3.2 do_page_f ...
- 如何系统学习C 语言(中)之 联合体、枚举篇
在C语言中有一个和结构体非常像的数据类型,它的名字叫做联合体,也被称为共用体或公用体. 1,联合体 1,联合体的定义 定义联合体需要使用"union" 关键字,格式如下: unio ...
- 【JavaScript定时器小案例】常见的几种定时器实现的案例
[JavaScript定时器小案例]常见的几种定时器实现的案例 博客说明 文章所涉及的资料来自互联网整理和个人总结,意在于个人学习和经验汇总,如有什么地方侵权,请联系本人删除,谢谢! 说明 在日常开发 ...
- [atAGC045D]Lamps and Buttons
由于$p_{i}$是随机的,不断选择最小的.未被操作过的$i$并处理其所在的环一定是最优的,而这样与已知$p_{i}$的区别是,当选择了一个$i=p_{i}$,那么必然失败(而已知$p_{i}$时不会 ...
- C/C++ Qt Dialog 对话框组件应用
在Qt中对话框分为两种形式,一种是标准对话框,另一种则是自定义对话框,在一般开发过程中标准对话框使用是最多的了,标准对话框一般包括 QMessageBox,QInputDialog,QFileDial ...
- Linux排序数据
1.sort 默认是按照字符大小来排序,如果要按照数字大小排序,需要加参数-n,-M按月排序 如:sort text.txt按字符大小排序 sort -n text.txt 按照数字大小排序 sort ...
- SpringCloud微服务实战——搭建企业级开发框架(二十五):实现多租户多平台短信通知服务
目前系统集成短信似乎是必不可少的部分,由于各种云平台都提供了不同的短信通道,这里我们增加多租户多通道的短信验证码,并增加配置项,使系统可以支持多家云平台提供的短信服务.这里以阿里云和腾讯云为例,集成短 ...