Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Two different Results between C and C++

54 views
Skip to first unread message

Real Troll

unread,
Jun 2, 2020, 10:13:40 AM6/2/20
to
Just noticed that these two program give two different results. One is a
C program and other is a C++ program.

<**********    C++   ******************>

#include <iostream>


using namespace std;

int main(){
   cout << sizeof('a') << endl;
   cout << sizeof(1!=1) << endl;
   return 0;
}

<**********    C   ******************>

#include<stdio.h>
#include <stdlib.h>
int main(){
   printf("%4zu", sizeof('a'));
   printf("%4zu", sizeof(1!=1));
   return 0;
}








Juha Nieminen

unread,
Jun 2, 2020, 2:45:18 PM6/2/20
to
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'.

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

Bart

unread,
Jun 2, 2020, 2:56:09 PM6/2/20
to
On 02/06/2020 19: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'.

Maybe because the type of 97 is int too. Then 'a' is just another way of
writing 97.

Lew Pitcher

unread,
Jun 2, 2020, 3:06:47 PM6/2/20
to
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"

Lew Pitcher

unread,
Jun 2, 2020, 3:11:30 PM6/2/20
to
Perhaps you are joking?

Everyone knows that the C character constant 'a' has a value of 129 (in
EBCDIC-US, of course).

James Kuyper

unread,
Jun 2, 2020, 4:20:25 PM6/2/20
to
On 6/2/20 2:45 PM, 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").

Because C developed from earlier languages where there was only one
type, and when C became a typed language, that type generally defaulted
to int. While objects can have an integer type that is smaller than int,
values of such types are, in almost all contexts, promoted to int. A
character literal has a value, but is not an object.

> ... Maybe
> it has something to do with C, at least originally, supporting
> multi-character constants like 'ab'.

It still does, and so does C++. Such constants have the type 'int' in
both languages.

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

I believe that the primary driver for the change was operator
overloading. Given

char c = 'c';

It would be inconvenient if overloaded_function(c) invoked a different
overload than overloaded_function('c').

Öö Tiib

unread,
Jun 2, 2020, 5:10:54 PM6/2/20
to
Weird, dinosaurs who use EBCDIC-037 usually say that 'a' is 0201.

Vir Campestris

unread,
Jun 4, 2020, 4:38:33 PM6/4/20
to
I last used EBCDIC on ICL2900s. Everything was hex, surprised to see octal.

Or lower case for that matter!

Andy

Öö Tiib

unread,
Jun 4, 2020, 5:26:35 PM6/4/20
to
Few who still maintain such things keep it arcane and cryptic.

Pavel

unread,
Jun 6, 2020, 12:07:42 AM6/6/20
to
for 1!=1, the type of relational operator in C++ is bool and in C it is int. -Pavel
0 new messages