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

发布于 2024-09-18 09:05:39 字数 36 浏览 14 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

走过海棠暮 2024-09-25 09:05:40

在 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 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.

眸中客 2024-09-25 09:05:40

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

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

这可能看起来像这样,例如:

const uint32_t png_ihdr = 'IHDR';

结果常量(在 GCC 中,它实现了这个)具有通过获取每个字符并将其向上移动而获得的值,以便“I”最终成为最重要的显然,如果您正在编写与平台无关的代码,则不应依赖于此。

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

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."

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.

一个人练习一个人 2024-09-25 09:05:40

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

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

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

char c = 'x';
char *s = "Hello World";
煮酒 2024-09-25 09:05:40
  • 'x'是一个整数,代表的数值
    机器字符集中的字母 x
  • "x" 是一个字符数组,两个字符长,
    'x' 后跟 '\0' 组成
  • '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’
如日中天 2024-09-25 09:05:40

我正在研究类似的东西: int cc = 'cc';它基本上是一个整数的字节复制。因此,看待它的方法是,基本上是 2 个 c 的“cc”被复制到整数 cc 的低 2 个字节。如果您正在寻找一个琐事,那么

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

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

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

99 25443

that's because 25443 = 99 + 256*99

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

Cheers

む无字情书 2024-09-25 09:05:40

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

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

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' };
清泪尽 2024-09-25 09:05:40
  1. 单引号用于字符
  2. 双引号用于字符串
  1. single quote is for character;
  2. double quote is for string.
梦年海沫深 2024-09-25 09:05:40

在 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

合久必婚 2024-09-25 09:05:40

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

char str[] = "Hello world";

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

char c = 'x';

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

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.

焚却相思 2024-09-25 09:05:40

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

例如...

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

输出

a  
Hello World

如果您反之亦然地使用它们,并对字符串使用单引号,对字符使用双引号,则结果将是:

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

输出:

对于第一行。您将得到垃圾值或意外值,或者可能得到如下输出:

而对于第二条语句,您将看不到任何内容。还有一件事,如果您在此之后有更多的陈述,他们也不会给您任何结果。

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

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.

塔塔猫 2024-09-25 09:05:40

使用单引号和单字符:

char ch = 'a';

这里 'a' 是一个 char 常量,等于 char a 的 ASCII 值。

将双引号与字符串一起使用,如下所示:

char str[] = "foo";

这里 "foo" 是字符串文字。

使用 "a" 可以,但使用 'foo' 不行

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.

Use double quote with strings as:

char str[] = "foo";

here "foo" is a string literal.

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

小ぇ时光︴ 2024-09-25 09:05:40

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

在Java中,也是一样的。

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

In Java, it is also the same.

飘逸的'云 2024-09-25 09:05:40

虽然我确信这并没有回答最初提问者的问题,但万一您最终在这里寻找像我一样的文字整数中的单引号...

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;

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.

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;
源来凯始玺欢你 2024-09-25 09:05:40

在C& C++ 单引号被称为字符 ('a'),而双引号被称为字符串 ("Hello")。不同之处在于,字符可以存储任何内容,但只能存储一个字母/数字等。字符串可以存储任何内容。
但还要记住“1”和 1 之间是有区别的。
如果您输入
cout<<'1'<

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

这次第一行将是 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

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:

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:

string s="Hi";
s+=48; //This will add "1" to the string
s+="1"; This will also add "1" to the string
内心旳酸楚 2024-09-25 09:05:40

声明字符/字符串的不同方式

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));

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));

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文