C++ “a”和“a”有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11279126/
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-27 15:00:35  来源:igfitidea点击:

What is the difference between 'a' and "a"?

c++stringchar

提问by james

I am learning C++ and have got a question that I cannot find the answer to.

我正在学习 C++ 并且遇到了一个我找不到答案的问题。

What is the difference between a charconstant (using single quotes) and a string constant (with double quotes)?

char常量(使用单引号)和字符串常量(使用双引号)有什么区别?

All my search results related to char arrays vs std::string. I am after the difference between 'a'and "a".

我所有的搜索结果都与 char 数组 vs std::string. 我之间的差额后'a'"a"

Would there be a difference in doing the following:

执行以下操作是否有区别:

cout << "a";
cout << 'a';

回答by Keith Thompson

'a'is a character literal. It's of type char, with the value 97 on most systems (the ASCII/Latin-1/Unicode encoding for the letter a).

'a'是字符文字。它的类型为char,在大多数系统上值为 97(字母的 ASCII/Latin-1/Unicode 编码a)。

"a"is a string literal. It's of type const char[2], and refers to an array of 2 chars with values 'a'and '\0'. In most, but not all, contexts, a reference to "a"will be implicitly converted to a pointer to the first character of the string.

"a"是字符串文字。它的类型为const char[2],并且指的是一个由 2 组成char的数组,其值为'a''\0'。在大多数(但不是全部)上下文中,对的引用"a"将隐式转换为指向字符串第一个字符的指针。

Both

两个都

cout << 'a';

and

cout << "a";

happen to produce the same output, but for different reasons. The first prints a single character value. The second successively prints all the characters of the string (except for the terminating '\0') -- which happens to be the single character 'a'.

碰巧产生相同的输出,但出于不同的原因。第一个打印单个字符值。第二个连续打印字符串的所有字符(终止符除外'\0')——恰好是单个字符'a'

String literals can be arbitrarily long, such as "abcdefg". Character literals almost always contain just a single character. (You can have multicharacter literals, such as 'ab', but their values are implementation-defined and they're very rarely useful.)

字符串文字可以是任意长的,例如"abcdefg". 字符文字几乎总是只包含一个字符。(您可以使用多字符文字,例如'ab',但它们的值是实现定义的,并且很少有用。)

(In C, which you didn't ask about, 'a'is of type int, and "a"is of type char[2](no const)).

(在 C 中,你没有问过,'a'是 type int,并且"a"char[2](no const)类型)。

回答by Ry-

"a"is an array of characters that just happens to only contain one character, or two if you count the \0at the end. 'a'is one character. They're not the same thing. For example:

"a"是一个字符数组,恰好只包含一个字符,或者如果你\0在最后算上两个字符。'a'是一个字符。他们不是一回事。例如:

#include <stdio.h>

void test(char c) {
    printf("Got character %c\n", c);
}

void test(const char* c) {
    printf("Got string %s\n", c);
}

int main() {
    test('c');
    test("c");
}

This will use two different overloads; see http://codepad.org/okl0UcCNfor a demo.

这将使用两种不同的重载;有关演示,请参阅http://codepad.org/okl0UcCN

回答by Hot Licks

Single quotes are used to surround character literals. Double quotes are used to surround string (character array) literals.

单引号用于包围字符文字。双引号用于包围字符串(字符数组)文字。

Many interfaces, such as cout <<, accept either.

许多接口,例如cout <<,都接受。

回答by ThomasMcLeod

A single quoted 'a'is a literal of type char. A double quoted "a"is a null-terminated string literal of chars.

单引号'a'是类型的文字char。双引号"a"chars 的以空字符结尾的字符串文字。

回答by user3425449

  • "a" -> it denotes it is a string. in c++ string is a collection of characters array.
  • so string is terminated by delimiter '\0'it indicates the end of string.
  • so its size would be 2 because 1-byte for "a" and 1-byte for '\0'
  • "a" -> 它表示它是一个字符串。在 C++ 中,字符串是字符数组的集合。
  • 所以字符串以分隔符 '\0'结尾,它表示字符串的结尾。
  • 所以它的大小是 2,因为 "a" 为 1 个字节,'\0' 为 1 个字节

where in case of 'a'-> it is single character. so its size would be 1-byte.

在 'a'-> 的情况下,它是单个字符。所以它的大小将是 1 字节。

char str[]="a"; 

or

或者

char *ptr = "c";

for  'c' -> char c = 'a';

or we can write as well

或者我们也可以写

char c = 97;

回答by ganga

'a' - 1) Character constant 2) size - 1 Character 3) Not a collection of Character array

'a' - 1) 字符常量 2) 大小 - 1 个字符 3) 不是字符数组的集合

"a" -1) String literals 2) size - 2 Character 3) collection of Character array

"a" -1) 字符串字面量 2) 大小 - 2 个字符 3) 字符数组的集合