为什么字符常量/常量不能为空?

时间:2021-05-19 09:37:22

In C and C++ the rules are the same. In C,

在C和c++中,规则是一样的。在C语言中,

[§6.4.4.4]/2 An integer character constant is a sequence of one or more multibyte characters enclosed in single-quotes, as in 'x'.

(§6.4.4.4)/ 2整数字符常数是一个序列的一个或多个多字节字符括在单引号,如“x”。

In C++,

在c++中,

[§2.14.3]/1 A character literal is one or more characters enclosed in single quotes, as in 'x', optionally preceded by one of the letters u, U, or L, as in u'y', U'z', or L'x', respectively.

(§2.14.3)/ 1字符文字是一个或多个字符括在单引号,如“x”,选择之前的一个字母u,u,或L,在你没有’,u 'z’,分别或L 'x”。

The key phrase is "one or more". In contrast, a string literal can be empty, "", presumably because it consists of the null terminating character. In C, this leads to awkward initialization of a char. Either you leave it uninitialized, or use a useless value like 0 or '\0'.

关键词是“一个或多个”。相反,字符串文字可以是空的,"",大概是因为它由空终止字符组成。在C中,这导致了一个字符的笨拙初始化。要么不初始化它,要么使用一个无用的值,比如0或'\0'。

char garbage;
char useless = 0;
char useless2 = '\0';

In C++, you have to use a string literal instead of a character literal if you want it to be empty.

在c++中,如果希望字符串为空,就必须使用字符串文字而不是字符文字。

(somecondition ? ' ' : '') // error
(somecondition ? " " : "") // necessary

What is the reason it is this way? I'm assuming C++'s reason is inherited from C.

这是为什么呢?我假设c++的原因是从C继承来的。

3 个解决方案

#1


10  

The reason is that a character literal is defined as a character. There may be extensions that allow it to be more than one character, but it needs to be at least one character or it just doesn't make any sense. It would be the same as trying to do:

原因是字符文字被定义为字符。可能有一些扩展,允许它不止一个字符,但是它至少需要一个字符,否则就没有任何意义。这与尝试做某事是一样的:

int i = ;

If you don't specify a value, what do you put there?

如果你不指定一个值,你在那里放什么?

#2


3  

This is because an empty string still contains the the null character '\0' at the end, so there is still a value to bind to the variable name, whereas an empty character literal has no value.

这是因为空字符串在末尾仍然包含空字符'\0',所以仍然有一个值要绑定到变量名,而空字符文字没有值。

#3


0  

String is a set of character terminated by a NULL character ( '\0' ). So a Empty string will always have a NULL character in it at the end .

字符串是由空字符('\0')终止的一组字符。所以一个空字符串最后总是会有一个空字符。

But in case of a character literal no value is there. it needs at least one character.

但是如果一个字符是文字,那么就没有值。它至少需要一个字符。

#1


10  

The reason is that a character literal is defined as a character. There may be extensions that allow it to be more than one character, but it needs to be at least one character or it just doesn't make any sense. It would be the same as trying to do:

原因是字符文字被定义为字符。可能有一些扩展,允许它不止一个字符,但是它至少需要一个字符,否则就没有任何意义。这与尝试做某事是一样的:

int i = ;

If you don't specify a value, what do you put there?

如果你不指定一个值,你在那里放什么?

#2


3  

This is because an empty string still contains the the null character '\0' at the end, so there is still a value to bind to the variable name, whereas an empty character literal has no value.

这是因为空字符串在末尾仍然包含空字符'\0',所以仍然有一个值要绑定到变量名,而空字符文字没有值。

#3


0  

String is a set of character terminated by a NULL character ( '\0' ). So a Empty string will always have a NULL character in it at the end .

字符串是由空字符('\0')终止的一组字符。所以一个空字符串最后总是会有一个空字符。

But in case of a character literal no value is there. it needs at least one character.

但是如果一个字符是文字,那么就没有值。它至少需要一个字符。