单引号与C或C+&#x2B中的双引号

发布于 2025-01-25 18:01:08 字数 34 浏览 2 评论 0原文

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

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

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

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

发布评论

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

评论(15

天冷不及心凉 2025-02-01 18:01:08

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

棒棒糖 2025-02-01 18:01:08

一些编译器还实施了允许多字符常数的扩展程序。 C99标准说:

6.4.4.4p10:“整数字符常数的值包含更多
比一个字符(例如,'ab')或
包含角色或逃脱
未映射到一个的序列
单字节执行字符,是
实现定义。”

例如,这可能是这样的:

const uint32_t png_ihdr = 'IHDR';

由此产生的常数(在GCC中,实现此功能)具有您通过拿每个字符并将其移动来获得的价值,因此'i 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.

书间行客 2025-02-01 18:01:08

单引号是字符(char),双引号为null终止字符串(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";
野心澎湃 2025-02-01 18:01:08
  • '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’
白鸥掠海 2025-02-01 18:01:08

我在围绕:int cc ='cc'的东西戳;碰巧的是,它基本上是一个字节副本到整数。因此,查看它的方法是,基本上是2 C的“ CC”被复制到整数CC的较低2个字节。如果您正在寻找琐事,那么

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

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

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

情释 2025-02-01 18:01:08

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

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' };
⊕婉儿 2025-02-01 18:01:08
  1. 单Quote用于targue;
  2. double Quote用于String
  1. single quote is for character;
  2. double quote is for string.
葵雨 2025-02-01 18:01:08

在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

梦魇绽荼蘼 2025-02-01 18:01:08

双引号是针对字符串文字的,例如:

char str[] = "Hello world";

单引号是针对单个字符文字的,例如:

char c = 'x';

编辑正如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.

ぽ尐不点ル 2025-02-01 18:01:08

字符使用单引号,而双引号则用于字符串。

例如...

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

输出

a  
Hello World

如果您在VICE VERSA案例中使用了这些,并将单个引号用于字符串和字符的双引号,这将是结果:

  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.

美人骨 2025-02-01 18:01:08

将单个引号与单个char一起使用:

char ch = 'a';

shere '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'

〗斷ホ乔殘χμё〖 2025-02-01 18:01:08

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

在Java中,也是一样的。

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

In Java, it is also the same.

稳稳的幸福 2025-02-01 18:01:08

虽然我敢肯定这不会回答原始询问的要求,但如果您最终在这里寻找像我这样的字面整数中的单个报价...

C ++ 14添加了添加 single quotes(')在数字中间一些视觉分组到数字。

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;
[浮城] 2025-02-01 18:01:08

在C&amp; C ++单引号称为字符('a'),而双引号被称为字符串(“ Hello”)。不同之处在于,一个字符可以存储任何东西,只有一个字母/数字等。字符串可以存储任何东西。
但也请记住,“ 1”和1之间存在区别。
如果您输入
cout&lt;&lt;'1'&lt;&lt; endl&lt;&lt; 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
泪之魂 2025-02-01 18:01:08

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

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 和您的相关数据。
原文