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 ...
随机推荐
- WPF进阶技巧和实战03-控件(4-基于范围的控件及日期控件)
系列文章链接 WPF进阶技巧和实战01-小技巧 WPF进阶技巧和实战02-布局 WPF进阶技巧和实战03-控件(1-控件及内容控件) WPF进阶技巧和实战03-控件(2-特殊容器) WPF进阶技巧和实 ...
- PyCharm Django Python 开发环境配置 详细教程
PyCharm Django Python 开发环境配置 详细教程 1. Python 下载及安装 (1)根据需要的版本去 Python 官网(https://www.python.org/downl ...
- 【JAVA】笔记(1)---JVM内存图;方法重载条件;输入方法;转义字符;强制类型转换;变量分类及区别;Java命名规范;
Java命名规范: 1.包:全部字母小写: 2.类+接口:所有单词的首字母大写: 3.变量+方法:第一个单词的首字母小写,其余单词首字母大写: 3.常量名:所有字母均大写,且用下划线" _ ...
- python接口之request测试:以json格式发送post请求,.json方法,查看响应结果的情况
json和dict python中的dict类型要转换为json格式的数据需要用到json库: import json <json> = json.dumps(<dict>) ...
- [bzoj1738]发抖的牛
二分答案,每一头牛向所有在规定时间内能走到的牛棚连inf的边,每一个源点向牛连牛数量的边,每一个牛棚向汇点连牛棚容量的边,能满流则意味着这个答案可行,否则不可行. 1 #include<bits ...
- 提升 RTC 音频体验 - 从搞懂硬件开始
前言 RTC(实时音视频通信)技术的快速发展,助力了直播.短视频等互动娱乐形式的普及:在全球疫情持续蔓延的态势下,云会议需求呈现爆发式增长,进一步推动了 RTC 行业的快速发展.为了给客户提供稳定可靠 ...
- 一类利用队列优化的DP
I.导入: 这是一个\(O(n^2)\)的状态和转移方程: \[f(i,j)=\left\{ \begin{aligned} f(i-1,j-1)+k \ (1\leq j)\\ \max_{k \i ...
- xshell的快捷复制粘贴设置
今天试着用xshell连接Linux,运行一些命令的时候想快点复制粘贴实现效率,却发现还要右键选择复制,再右键选择粘贴,很是麻烦. 看了一下xshell的设置,其实可以自己设置成快捷方式 以xshel ...
- 金蝶EAS——我的EAS报销流程怎么能让另一个人看到呢?即如何设置流程传阅功能?设置“代理报销”
代理的话只能看到被代理人能看到的流程.设置"代理报销":应用--财务会计--费用管理--代理报销 选择报销人公司--"他人代理我报销"--选择报销人(zhaof ...
- web性能测试工具——http_load
http_load是一款基于Linux平台的web服务器性能测试工具,用于测试web服务器的吞吐量与负载,web页面的性能. http_load是基于linux.unix平台的一种性能测工具 它以并行 ...