Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there no empty char literal?

Is there any specific reason why there is no empty char literal?

What comes closest to what I think of, the '' is the '\0' the null character.

In C++ the char is represented by an int, which means empty char goes directly to the 0 integer value, which is in C++ "the same as null".

The practical part of coming up with that question:

In a class I want to represent char values as enum attributes. Unbiased I tried to initialize an instance with '', which of course does not work. But shouldn't be there a char null value? Not to be confused with string.Empty, more in the nature of a null reference.

So the question is: Why is there no empty char?

-edit-

Seeing this question the question can be enhanced on: An empty char value would enable concatening strings and chars without destroying the string. Would that not be preferable? Or should this "just work as expected"?

like image 340
Mare Infinitus Avatar asked Oct 17 '25 18:10

Mare Infinitus


2 Answers

A char by definition has a length of one character. Empty simply doesn't fit the bill.

Don't run into confusion between a char and a string of max length 1. They sure look similar, but are very different beasts.

like image 128
Eugen Rieck Avatar answered Oct 19 '25 08:10

Eugen Rieck


To give a slightly more technical explanation: There is no character that can serve as the identity element when performing concatenation. This is different from integers, where 0 serves as the identity element for addition.

like image 28
Douglas Avatar answered Oct 19 '25 07:10

Douglas