[C-prog-lang-l] why is the value of character constant an integer

Jan Pechanec jp at devnull.cz
Sun Feb 26 13:02:23 CET 2023


On Tue, 21 Feb 2023, Vladimír Kotal wrote:

>Hi all,
>
>someone asked on Monday after the lecture why is the value of 
>character constant an int (rather than char).
>
>I think this is historical and for flexibility/compatibility. For 
>instance, multi-character constants (e.g. 'ABC' - with single quotes) 
>_usually_ (implementation defined - another type of behavior) result 
>in values larger than a char (signed or unsigned) can hold.
>
>By the way, you can verify compiler behavior on godbolt.org, it lets 
>you select various compilers and versions. Here's one for the 
>multi-char constant: https://godbolt.org/z/zjWej99xc
>
>Most of the compilers available there implement the value the same, 
>but not all of them (try the Chibicc compiler).

As we will see later in the semester, char/short values are usually 
promoted to an int anyway, even in variadic functions:

char c;
printf("%d\n", c);  // 4 bytes are put onto a stack or into a reg

So IMO it made sense to keep it an int.

Jan

-- 
Jan Pechanec <jp (at) devnull (dot) cz>
http://www.devnull.cz


More information about the c-prog-lang-l mailing list