C 或 C++ 中的单引号与双引号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3683602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 13:28:08  来源:igfitidea点击:

Single quotes vs. double quotes in C or C++

c++csyntaxcharstring-literals

提问by mr_eclair

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

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

回答by David Rodríguez - dribeas

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'和一个空终止符的字符串文字(即一个 2 个字符的数组)。

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,即sizeof 'a'在 int 为 32 位(且 CHAR_BIT 为 8)的体系结构中为 4,而sizeof(char)在任何地方均为 1。

回答by unwind

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.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 位值的最高有效位。显然,如果您正在编写独立于平台的代码,则不应依赖于此。

回答by Eiko

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

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

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

回答by martynas

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

回答by sanket

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'; 碰巧它基本上是对整数的逐字节复制。因此,看待它的方法是将基本上是 2 个 c 的 'cc' 复制到整数 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 nota string.

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

Cheers

干杯

回答by Graham Borland

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','
char str[] = "Hello world";
' };

回答by bygreencn

  1. single quoteis for character;
  2. double quoteis for string.
  1. single quote是为了character
  2. double quote是为了string

回答by Jens Gustedt

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 字符结尾

回答by Oliver Charlesworth

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

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

char c = 'x';

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

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

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

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

编辑正如大卫在另一个答案中所说,字符文字的类型是int.

回答by Hafiz Shehbaz Ali

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;

如果您在反之亦然的情况下使用这些,并且对字符串使用单引号,对字符使用双引号。在这里,这将是结果;

##代码##

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 语言使您可以灵活地轻松使用单引号和双引号。