C 或 C++ 中的单引号与双引号
在 C 或 C++ 编程中什么时候应该使用单引号和双引号?
When should I use single quotes and double quotes in C or C++ programming?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
在 C 或 C++ 编程中什么时候应该使用单引号和双引号?
When should I use single quotes and double quotes in C or C++ programming?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(15)
在 C 和 C++ 中,单引号标识单个字符,而双引号创建字符串文字。
'a'
是单个 a 字符文字,而"a"
是包含'a'
和空终止符(即是一个 2 个字符的数组)。在 C++ 中,字符文字的类型是
char
,但请注意,在 C 中,字符文字的类型是int
,即sizeof 'a'<在整数为 32 位(CHAR_BIT 为 8)的体系结构中, /code> 为 4,而
sizeof(char)
在任何地方都为 1。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 isint
, that issizeof 'a'
is 4 in an architecture where ints are 32bit (and CHAR_BIT is 8), whilesizeof(char)
is 1 everywhere.一些编译器还实现了允许多字符常量的扩展。 C99标准说:
这可能看起来像这样,例如:
结果常量(在 GCC 中,它实现了这个)具有通过获取每个字符并将其向上移动而获得的值,以便“I”最终成为最重要的显然,如果您正在编写与平台无关的代码,则不应依赖于此。
Some compilers also implement an extension, that allows multi-character constants. The C99 standard says:
This could look like this, for instance:
The resulting constant (in GCC, which implements this) has the value you get by taking each character and shifting it up, so that 'I' ends up in the most significant bits of the 32-bit value. Obviously, you shouldn't rely on this if you are writing platform independent code.
单引号是字符 (
char
),双引号是空终止字符串 (char *
)。Single quotes are characters (
char
), double quotes are null-terminated strings (char *
).'x'
是一个整数,代表的数值机器字符集中的字母 x
"x"
是一个字符数组,两个字符长,由
'x'
后跟'\0'
组成'x'
is an integer, representing the numerical value of theletter x in the machine’s character set
"x"
is an array of characters, two characters long,consisting of
‘x’
followed by‘\0’
我正在研究类似的东西: int cc = 'cc';它基本上是一个整数的字节复制。因此,看待它的方法是,基本上是 2 个 c 的“cc”被复制到整数 cc 的低 2 个字节。如果您正在寻找一个琐事,那么
99 25443
那是因为 25443 = 99 + 256*99
所以“cc”是一个多字符常量,而不是一个字符串。
干杯
I was poking around stuff like: int cc = 'cc'; It happens that it's basically a byte-wise copy to an integer. Hence the way to look at it is that 'cc' which is basically 2 c's are copied to lower 2 bytes of the integer cc. If you are looking for a trivia, then
99 25443
that's because 25443 = 99 + 256*99
So 'cc' is a multi-character constant and not a string.
Cheers
单引号用于单个字符。双引号用于字符串(字符数组)。如果您愿意,可以使用单引号一次一个字符地构建字符串。
Single quotes are for a single character. Double quotes are for a string (array of characters). You can use single quotes to build up a string one character at a time, if you like.
单引号
用于字符
;双引号
用于字符串
。single quote
is forcharacter
;double quote
is forstring
.在 C 中,单引号如
'a'
表示字符常量,而"a"
是字符数组,始终以\0
人物In C, single-quotes such as
'a'
indicate character constants whereas"a"
is an array of characters, always terminated with the\0
character双引号用于字符串文字,例如:
单引号用于单字符文字,例如:
EDIT 正如 David 在另一个答案中所述,字符文字的类型是
int
。Double quotes are for string literals, e.g.:
Single quotes are for single character literals, e.g.:
EDIT As David stated in another answer, the type of a character literal is
int
.单引号用于字符,双引号用于字符串。
例如...
输出
如果您反之亦然地使用它们,并对字符串使用单引号,对字符使用双引号,则结果将是:
输出:
对于第一行。您将得到垃圾值或意外值,或者可能得到如下输出:
而对于第二条语句,您将看不到任何内容。还有一件事,如果您在此之后有更多的陈述,他们也不会给您任何结果。
注意:PHP 语言使您可以灵活地轻松使用单引号和双引号。
A single quote is used for character, while double quotes are used for strings.
For example...
Output
If you used these in vice versa case and used a single quote for string and double quotes for a character, this will be the result:
output :
For the first line. You will get a garbage value or unexpected value or you may get an output like this:
While for the second statement, you will see nothing. One more thing, if you have more statements after this, they will also give you no result.
Note: PHP language gives you the flexibility to use single and double-quotes easily.
使用单引号和单字符:
这里
'a'
是一个 char 常量,等于 char a 的ASCII
值。将双引号与字符串一起使用,如下所示:
这里
"foo"
是字符串文字。使用
"a"
可以,但使用 'foo'
不行Use single quote with single char as:
here
'a'
is a char constant and is equal to theASCII
value of char a.Use double quote with strings as:
here
"foo"
is a string literal.Its okay to use
"a"
but its not okay to use 'foo'
单引号表示字符,双引号表示字符串。
在Java中,也是一样的。
Single quotes are denoting a char, double denote a string.
In Java, it is also the same.
虽然我确信这并没有回答最初提问者的问题,但万一您最终在这里寻找像我一样的文字整数中的单引号...
C++14 添加了添加 单引号 (
'
) 在数字文字中间 添加对数字进行一些视觉分组。While I'm sure this doesn't answer what the original asker asked, in case you end up here looking for single quote in literal integers like I have...
C++14 added the ability to add single quotes (
'
) in the middle of number literals to add some visual grouping to the numbers.在C& C++ 单引号被称为字符 ('a'),而双引号被称为字符串 ("Hello")。不同之处在于,字符可以存储任何内容,但只能存储一个字母/数字等。字符串可以存储任何内容。
但还要记住“1”和 1 之间是有区别的。
如果您输入
cout<<'1'<
这次第一行将是 48。当您将字符转换为 int 时,它会转换为其 ascii,而“1”的 ascii 是 48。
同样,如果你这样做:
In C & C++ single quotes is known as a character ('a') whereas double quotes is know as a string ("Hello"). The difference is that a character can store anything but only one alphabet/number etc. A string can store anything.
But also remember that there is a difference between '1' and 1.
If you type
cout<<'1'<<endl<<1;
The output would be the same, but not in this case:
This time the first line would be 48. As when you convert a character to an int it converts to its ascii and the ascii for '1' is 48.
Same, if you do:
声明字符/字符串的不同方式
different way to declare a char / string