On June 2, 2020 14:45, Juha Nieminen wrote:
> In comp.lang.c++ Real Troll <
real....@trolls.com> wrote:
>> Just noticed that these two program give two different results. One is a
>> C program and other is a C++ program.
>
> The type of 'a' in C is int, while in C++ it's char. I'm not exactly
> sure why its type is int in C (given that C has always had char as a
> type, and it has, too, mostly been designed from the ground up with
> the philosophy of "you don't pay for what you don't use"). Maybe
> it has something to do with C, at least originally, supporting
> multi-character constants like 'ab'.
From what I have been able to find, multi-character constants initiallly
were a platform-dependant side-effect of how K&R C handled character
constants.
I believe that the reason for K&R C handling character constants as integers
revolves around the original compiler constraints in emitting PDP-11
assembly/machine language. Specifically, Dennis Ritchie, in the "C Reference
Manual" (circa May 1975) states, of character constants, that
"they behave exactly like integers (not, in particular, like objects of
character type). In conformity with the addressing structure of the
PDP-11, a character constant of length 1 has the code for the given
character in the low-order byte and 0 in the high-order byte; a character
constant of length 2 has the code for the first character in the low byte
and that for the second character in the high-order byte. Character
constants with more than one character are inherently machine-dependent
and should be avoided."
(
https://www.bell-labs.com/usr/dmr/www/cman.pdf)
>
> I'm also not completely certain why this is one thing which C++
> decided to change. (I suppose it's good that it changed it, but
> I'm not really sure why this.)
--
Lew Pitcher
"In Skills, We Trust"