单引号与C或C+&#x2B中的双引号
我什么时候应该在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'a''
和一个null终结者(是一个2个字符阵列)。在C ++中,字符的类型是
char
,但请注意,在C中,字符字体的类型是int
,即sizeof'A'a'a'< /code>在ints为32位(和char_bit是8)的体系结构中为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 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
),双引号为null终止字符串(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
so'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.
单Quote
用于targue
;double Quote
用于String
。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双引号是针对字符串文字的,例如:
单引号是针对单个字符文字的,例如:
编辑正如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
.字符使用单引号,而双引号则用于字符串。
例如...
输出
如果您在VICE VERSA案例中使用了这些,并将单个引号用于字符串和字符的双引号,这将是结果:
输出:
对于第一行。您将获得垃圾价值或意外值,否则您可能会得到这样的输出:
对于第二个语句,您将一无所获。还有一件事,如果您在此之后有更多的陈述,他们也不会给您带来任何结果。
注意: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.
将单个引号与单个char一起使用:
shere
'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添加了添加 single quotes(
'
)在数字中间一些视觉分组到数字。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&amp; C ++单引号称为字符('a'),而双引号被称为字符串(“ Hello”)。不同之处在于,一个字符可以存储任何东西,只有一个字母/数字等。字符串可以存储任何东西。
但也请记住,“ 1”和1之间存在区别。
如果您输入
cout&lt;&lt;'1'&lt;&lt; endl&lt;&lt; 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:
声明char / string的不同方式
different way to declare a char / string