C/C++中单引号与双引号使用

一直使用单引号和双引号,在Python中好像没有很强的使用限定,但是看到C++中两者的使用有些不同。

参考:
Single quotes vs. double quotes in C or C++

In C and in C++ single quotes identify a single character, while double quotes create a string literal. ‘a’ is a single a character literal, while “a” is a string literal containing an ‘a’ and a null terminator (that is a 2 char array).

In C++ the type of a character literal is char, but note that in C, the type of a character literal is int, that is sizeof ‘a’ is 4 in an architecture where ints are 32bit (and CHAR_BIT is 8), while sizeof(char) is 1 everywhere.

意思就是:
1)单引号:单个字符,双引号:一个字符串常量;
2)双引号表示的字符串常量包含结束字符'\0'

例如,"a"包含 a 和 \0 两个字符。

3)C++中单个字符是char型,1个字节
C中单个字符是int型,4个字节(32位)。


【注】:
1)C/C++中每个字符串都是以字符\0作为结尾,每个字符串都有一个额外字符的开销,需要注意字符串的越界情况。
2)指针赋值给相同的常量字符串时,实际指向相同的内存地址;
但常量内存初始化数组,不是指向相同的地址。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值