简体   繁体   English

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

[英]Single quotes vs. double quotes in C or C++

When should I use single quotes and double quotes in C or C++ programming? C或C++编程什么时候用单引号和双引号?

In C and in C++ single quotes identify a single character, while double quotes create a string literal.在 C 和 C++ 中,单引号标识单个字符,而双引号创建字符串文字。 '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). '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。

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 (eg, '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.生成的常量(在 GCC 中实现了这个)具有通过获取每个字符并将其向上移动获得的值,因此“I”最终位于 32 位值的最高有效位中。 Obviously, you shouldn't rely on this if you are writing platform independent code.显然,如果您正在编写独立于平台的代码,则不应该依赖它。

Single quotes are characters ( char ), double quotes are null-terminated strings ( char * ).单引号是字符( char ),双引号是以空字符结尾的字符串( char * )。

char c = 'x';
char *s = "Hello World";
  • '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'组成

I was poking around stuff like: int cc = 'cc';我正在寻找类似的东西: 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.因此,查看它的方法是将基本上是 2 个 c 的“cc”复制到整数 cc 的低 2 个字节。 If you are looking for a trivia, then如果您正在寻找琐事,那么

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干杯

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' };
  1. single quote is for character ; single quote用于character
  2. double quote is for string . double quote用于string

在 C 中,单引号(例如'a'表示字符常量,而"a"是一个字符数组,总是以\0字符结尾

Double quotes are for string literals, eg:双引号用于字符串文字,例如:

char str[] = "Hello world";

Single quotes are for single character literals, eg:单引号用于单字符文字,例如:

char c = 'x';

EDIT As David stated in another answer, the type of a character literal is int .编辑正如大卫在另一个答案中所说,字符文字的类型是int

A single quote is used for character, while double quotes are used for strings.单引号用于字符,而双引号用于字符串。

For example...例如...

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

Output输出

a  
Hello World

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:如果您在反之亦然的情况下使用这些,并对字符串使用单引号,对字符使用双引号,结果如下:

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

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

Single quotes are denoting a char, double denote a string.单引号表示字符,双引号表示字符串。

In Java, it is also the same.在 Java 中也是如此。

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'是一个 char 常量,等于 char 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'

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++14 添加了在数字文字中间添加单引号 ( ' ) 的功能,以便为数字添加一些视觉分组。

constexpr int oneBillion = 1'000'000'000;
constexpr int binary = 0b1010'0101;
constexpr int hex = 0x12'34'5678;
constexpr double pi = 3.1415926535'8979323846'2643383279'5028841971'6939937510;

In C & C++ single quotes is known as a character ('a') whereas double quotes is know as a string ("Hello").在 C 和 C++ 中,单引号称为字符 ('a'),而双引号称为字符串 ("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;但也要记住'1'和1之间是有区别的。如果你输入cout<<'1'<<endl<<1; The output would be the same, but not in this case:输出将是相同的,但在这种情况下不同:

cout<<int('1')<<endl<<int(1);

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:这次第一行是 48。当你将一个字符转换为 int 时,它会转换为它的 ascii,而 '1' 的 ascii 是 48。同样,如果你这样做:

string s="Hi";
s+=48; //This will add "1" to the string
s+="1"; This will also add "1" to the string

different way to declare a char / string声明字符/字符串的不同方式

char char_simple = 'a'; // bytes 1 : -128 to 127 or 0 to 255
signed char char_signed = 'a'; // bytes 1: -128 to 127
unsigned char char_u = 'a';  // bytes 2: 0 to 255

// double quote is for string.
char string_simple[] = "myString";
char string_simple_2[] = {'m', 'S', 't', 'r', 'i', 'n', 'g'};
char string_fixed_size[8] = "myString";
char *string_pointer = "myString"; 
char string_poionter_2 = *"myString";

printf("char = %ld\n", sizeof(char_simple));
printf("char_signed = %ld\n", sizeof(char_signed));
printf("char_u = %ld\n", sizeof(char_u));

printf("string_simple[] = %ld\n", sizeof(string_simple));
printf("string_simple_2[] = %ld\n", sizeof(string_simple_2));
printf("string_fixed_size[8] = %ld\n", sizeof(string_fixed_size));
printf("*string_pointer = %ld\n", sizeof(string_pointer));
printf("string_poionter_2 = %ld\n", sizeof(string_poionter_2));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM