C或c++中的单引号和双引号

时间:2022-09-15 13:37:36

When should I use single quotes and double quotes in C or C++ programming?

在C或c++编程中,什么时候应该使用单引号和双引号?

12 个解决方案

#1


198  

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和c++中,单引号标识一个字符,而双引号创建一个字符串文字。“a”是一个字符文本,而“a”是包含“a”和“空终止符”(即两个字符数组)的字符串文本。

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++中,字符文字的类型是char,但请注意,在C中,字符文字的类型是int,在ints为32位(CHAR_BIT为8)的体系结构中,是sizeof 'a'是4,而sizeof(char)是1。

#2


37  

Some compilers also implement an extension, that allows multi-character constants. The C99 standard says:

一些编译器还实现了一个允许多字符常量的扩展。C99标准说:

6.4.4.4p10: "The value of an integer character constant containing more than one character (e.g., 'ab'), or containing a character or escape sequence that does not map to a single-byte execution character, is implementation-defined."

6.4.4p10:“包含多个字符(例如'ab')或包含不映射到单字节执行字符的字符或转义序列的整数字符常量的值是实现定义的。”

This could look like this, for instance:

这可能是这样的,例如:

const uint32_t png_ihdr = 'IHDR';

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.

得到的常量(在GCC中,它实现了这个)具有您通过获取每个字符并将其向上移动而获得的值,因此“I”最终以32位值中最重要的位结束。显然,如果您正在编写平*立的代码,那么您不应该依赖它。

#3


22  

Single quotes are characters (char), double quotes are null-terminated strings (char *).

单引号是字符(char),双引号是空终止字符串(char *)。

char c = 'x';
char *s = "Hello World";

#4


9  

  • 'x' is an integer, representing the numerical value of the letter x in the machine’s character set
  • “x”是一个整数,表示机器字符集中字母x的数值。
  • "x" is an array of characters, two characters long, consisting of ‘x’ followed by ‘\0’
  • “x”是一个字符数组,两个字符长,由“x”组成,后面是“\0”

#5


6  

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.

单引号用于单个字符。双引号用于字符串(字符数组)。如果您愿意,可以使用单引号每次构建一个字符串。

char myChar     = 'A';
char myString[] = "Hello Mum";
char myOtherString[] = { 'H','e','l','l','o','\0' };

#6


6  

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

我到处找,比如:int cc = 'cc';它基本上是一个整数的字节拷贝。因此,我们可以这样看,“cc”也就是2个c被复制来减少整数cc的2个字节

printf("%d %d", 'c', 'cc'); would give:

99 25443

99 25443

that's because 25443 = 99 + 256*99

那是因为25443 = 99 + 256*99

So 'cc' is a multi-character constant and not a string.

所以cc是一个多字符常量,而不是字符串。

Cheers

干杯

#7


5  

Double quotes are for string literals, e.g.:

双引号用于字符串形式,例如:

char str[] = "Hello world";

Single quotes are for single character literals, e.g.:

单引号用于单个字符的文字,例如:

char c = 'x';

EDIT As David stated in another answer, the type of a character literal is int.

正如David在另一个答案中所述,字符文本的类型是int。

#8


5  

In C single quotes such as 'a' indicate character constants whereas "a" is an array of characters, always terminated with the 0 character

在C语言中,例如“a”表示字符常量,而“a”表示字符数组,总是以0字符结尾

#9


4  

  1. single quote is for character;
  2. 单引号表示字符;
  3. double quote is for string.
  4. 双引号表示字符串。

#10


3  

Single quotes are denoting a char, double denote a string.

单引号表示字符,双引号表示字符串。

In Java, it is also the same.

在Java中,也是一样的。

#11


2  

Use single quote with single char as:

使用单引号单引号:

char ch = 'a';

here 'a' is a char constant and is equal to the ASCII value of char a.

这里的a是一个字符常数,等于字符a的ASCII值。

Use double quote with strings as:

使用带有字符串的双引号:

char str[] = "foo";

here "foo" is a string literal.

这里的foo是一个字符串字面量。

Its okay to use "a" but its not okay to use 'foo'

可以使用"a"但不能用" foo "

#12


2  

Single quote is used for character, while double quote used for string.

单引号用于字符,双引号用于字符串。

For example..

 printf("%c \n",'a');
 printf("%s","Hello World");

Output

a Hello World

你好,世界

If you used these in vice versa case and used single quote for string and double quote for character. Here, this will be result;

如果你在相反的情况下使用这些,用单引号表示字符串,用双引号表示字符。这就是结果;

  printf("%c \n","a");
  printf("%s",'Hello World');

output :

for first line.You will have garbage value or unexpected.or you may be have output like this..

第一行。您将得到垃圾值或意外值。或者你可能有这样的输出。

while for the second statement. You will see nothing. One thing more. If you have more statement after this. They will also give you no result.

而对于第二个语句。你会看到什么。一件事。如果你有更多的陈述。他们也不会给你任何结果。

Note : PHP language give you flexibility to use single and double quote easily.

注意:PHP语言可以灵活地使用单引号和双引号。

#1


198  

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和c++中,单引号标识一个字符,而双引号创建一个字符串文字。“a”是一个字符文本,而“a”是包含“a”和“空终止符”(即两个字符数组)的字符串文本。

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++中,字符文字的类型是char,但请注意,在C中,字符文字的类型是int,在ints为32位(CHAR_BIT为8)的体系结构中,是sizeof 'a'是4,而sizeof(char)是1。

#2


37  

Some compilers also implement an extension, that allows multi-character constants. The C99 standard says:

一些编译器还实现了一个允许多字符常量的扩展。C99标准说:

6.4.4.4p10: "The value of an integer character constant containing more than one character (e.g., 'ab'), or containing a character or escape sequence that does not map to a single-byte execution character, is implementation-defined."

6.4.4p10:“包含多个字符(例如'ab')或包含不映射到单字节执行字符的字符或转义序列的整数字符常量的值是实现定义的。”

This could look like this, for instance:

这可能是这样的,例如:

const uint32_t png_ihdr = 'IHDR';

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.

得到的常量(在GCC中,它实现了这个)具有您通过获取每个字符并将其向上移动而获得的值,因此“I”最终以32位值中最重要的位结束。显然,如果您正在编写平*立的代码,那么您不应该依赖它。

#3


22  

Single quotes are characters (char), double quotes are null-terminated strings (char *).

单引号是字符(char),双引号是空终止字符串(char *)。

char c = 'x';
char *s = "Hello World";

#4


9  

  • 'x' is an integer, representing the numerical value of the letter x in the machine’s character set
  • “x”是一个整数,表示机器字符集中字母x的数值。
  • "x" is an array of characters, two characters long, consisting of ‘x’ followed by ‘\0’
  • “x”是一个字符数组,两个字符长,由“x”组成,后面是“\0”

#5


6  

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.

单引号用于单个字符。双引号用于字符串(字符数组)。如果您愿意,可以使用单引号每次构建一个字符串。

char myChar     = 'A';
char myString[] = "Hello Mum";
char myOtherString[] = { 'H','e','l','l','o','\0' };

#6


6  

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

我到处找,比如:int cc = 'cc';它基本上是一个整数的字节拷贝。因此,我们可以这样看,“cc”也就是2个c被复制来减少整数cc的2个字节

printf("%d %d", 'c', 'cc'); would give:

99 25443

99 25443

that's because 25443 = 99 + 256*99

那是因为25443 = 99 + 256*99

So 'cc' is a multi-character constant and not a string.

所以cc是一个多字符常量,而不是字符串。

Cheers

干杯

#7


5  

Double quotes are for string literals, e.g.:

双引号用于字符串形式,例如:

char str[] = "Hello world";

Single quotes are for single character literals, e.g.:

单引号用于单个字符的文字,例如:

char c = 'x';

EDIT As David stated in another answer, the type of a character literal is int.

正如David在另一个答案中所述,字符文本的类型是int。

#8


5  

In C single quotes such as 'a' indicate character constants whereas "a" is an array of characters, always terminated with the 0 character

在C语言中,例如“a”表示字符常量,而“a”表示字符数组,总是以0字符结尾

#9


4  

  1. single quote is for character;
  2. 单引号表示字符;
  3. double quote is for string.
  4. 双引号表示字符串。

#10


3  

Single quotes are denoting a char, double denote a string.

单引号表示字符,双引号表示字符串。

In Java, it is also the same.

在Java中,也是一样的。

#11


2  

Use single quote with single char as:

使用单引号单引号:

char ch = 'a';

here 'a' is a char constant and is equal to the ASCII value of char a.

这里的a是一个字符常数,等于字符a的ASCII值。

Use double quote with strings as:

使用带有字符串的双引号:

char str[] = "foo";

here "foo" is a string literal.

这里的foo是一个字符串字面量。

Its okay to use "a" but its not okay to use 'foo'

可以使用"a"但不能用" foo "

#12


2  

Single quote is used for character, while double quote used for string.

单引号用于字符,双引号用于字符串。

For example..

 printf("%c \n",'a');
 printf("%s","Hello World");

Output

a Hello World

你好,世界

If you used these in vice versa case and used single quote for string and double quote for character. Here, this will be result;

如果你在相反的情况下使用这些,用单引号表示字符串,用双引号表示字符。这就是结果;

  printf("%c \n","a");
  printf("%s",'Hello World');

output :

for first line.You will have garbage value or unexpected.or you may be have output like this..

第一行。您将得到垃圾值或意外值。或者你可能有这样的输出。

while for the second statement. You will see nothing. One thing more. If you have more statement after this. They will also give you no result.

而对于第二个语句。你会看到什么。一件事。如果你有更多的陈述。他们也不会给你任何结果。

Note : PHP language give you flexibility to use single and double quote easily.

注意:PHP语言可以灵活地使用单引号和双引号。