C 或 C++ 中的单引号与双引号 [英] Single quotes vs. double quotes in C or C++
问题描述
我应该在 C 或 C++ 编程中什么时候使用单引号和双引号?
When should I use single quotes and double quotes in C or C++ programming?
推荐答案
在 C 和 C++ 中,单引号标识单个字符,而双引号创建字符串文字.'a'
是单个字符文字,而 "a"
是包含 'a'
和空终止符(即是一个 2 个字符的数组).
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).
在 C++ 中,字符字面量的类型是 char
,但请注意,在 C 中,字符字面量的类型是 int
,即 sizeof '在 int 为 32 位(CHAR_BIT 为 8)的架构中,a'
为 4,而 sizeof(char)
处处为 1.
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.
这篇关于C 或 C++ 中的单引号与双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!