博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
整型字符常量和字符字面量的区别 sizeof(char) 和 sizeof('a')
阅读量:2386 次
发布时间:2019-05-10

本文共 500 字,大约阅读时间需要 1 分钟。

编译和执行如下c语言代码,系统将会输出什么?
1
2
3
4
5
6
7
#include<stdio.h>
int
main()
{
    
char
c=
'0'
;
    
printf
(
"%d %d"
,
sizeof
(c),
sizeof
(
'0'
));
    
return
0;
}

正确答案: A   你的答案: C (错误)

1 4
2 2
1 1
2 1

C语言: char a = 'a'; sizeof(char) = 1 sizeof(a) = 1 sizeof('a') = 4 
C++语言: char a = 'a'; sizeof(char) = 1 sizeof(a) = 1 sizeof('a') = 1 
字符型变量是1字节这个没错,奇怪就奇怪在C语言认为'a'是4字节,而C++语言认为'a'是1字节。 
     
原因如下:  
  C99标准的规定,'a'叫做整型字符常量(integer    character constant),被看成是int型,所以在32位机器上占4字节。
  ISO C++标准规定,'a'叫做字符字面量(character literal),被看成是char型,所以占1字节

转载地址:http://xziab.baihongyu.com/

你可能感兴趣的文章
hadoop 绑定到IPV4上
查看>>
TRS WCM SQL injection
查看>>
Resin远程信息泄露漏洞
查看>>
[waraxe-2013-SA#103] - Multiple Vulnerabilities in phpMyAdmin
查看>>
sockscap
查看>>
Having fun web crawling with phantomJs
查看>>
Web Development – Installing mod_security with OWASP
查看>>
toolsmith: Recon-ng
查看>>
BF and IA vulnerabilities in IBM Lotus Domino
查看>>
php分页代码
查看>>
Yii 分页方法总结
查看>>
Active Directory Password Hash Extraction
查看>>
ColdFusion 9 / 10 Remote Root xday
查看>>
Exim / Dovecot Command Execution
查看>>
sudo 1.8.3p1 Local Root
查看>>
TinyMCE Ajax File Manager suffers from a remote code execution vulnerability.
查看>>
SMF 2.0.4 PHP Code Injection
查看>>
Financial Cyber Security
查看>>
SAP SOAP RFC SXPG_COMMAND_EXECUTE Remote Command Execution
查看>>
SAP SOAP RFC SXPG_CALL_SYSTEM Remote Command Execution
查看>>