“a"和“a"有什么区别?

What is the difference between #39;a#39; and quot;aquot;?(“a和“a有什么区别?)

本文介绍了“a"和“a"有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 C++,但遇到了一个我找不到答案的问题.

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

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

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

我所有的搜索结果都与 char 数组 vs std::string 相关.我在寻找 'a'"a" 之间的区别.

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

执行以下操作会有区别吗:

Would there be a difference in doing the following:

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

推荐答案

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

'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" 是字符串文字.它是 const char[2] 类型,并引用 2 个 char 的数组,其值为 'a'''.在大多数(但不是全部)上下文中,对 "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 ''. In most, but not all, contexts, a reference to "a" will be implicitly converted to a pointer to the first character of the string.

两者

cout << 'a';

cout << "a";

碰巧产生相同的输出,但出于不同的原因.第一个打印单个字符值.第二个连续打印字符串的所有字符(终止 '' 除外)——恰好是单个字符 '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 '') -- which happens to be the single character 'a'.

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

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

(在 C 中,您没有问过,'a' 属于 int 类型,而 "a" 属于输入 char[2](没有 const)).

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

这篇关于“a"和“a"有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:“a"和“a"有什么区别?

基础教程推荐