Some of the things you've said are true, some are mistaken. Let's go through them in turn.
char x='b';
(x
has two values one of them is int
and the other is char
in the two languages ( C and C++))
Well, no, not really. When you say char x
you get a variable generally capable of holding one character, and this is perfectly, equally true in both C and C++. The only difference, as we'll see, is the type (not the value) of the character constant 'b'
.
in C if I let the compiler choose
I'm not sure what you mean by "choose", because there's not really any choice in the matter.
it will choose (Will give priority to) the int
value like in this code
printf("%d", sizeof('b'));
output is: 4 (int size) that is meaning the compiler change b to 98 first and get the size it as int
Not exactly. You got 4 because, yes, the type of a character constant like 'b'
is int
. In ASCII, the character constant 'b'
has the value 98 no matter what. The compiler didn't change a character to an int here, and the value didn't change from 'b'
to 98 here. It was an int all along (because that's the type of character constants in C), and it had the value 98 all along (because that's the value of the letter b
in ASCII).
but I can use the char value if I choose like in this code
`printf("%c", x);
Right. But there's nothing magical about that. Consider:
char c1 = 'b';
int i1 = 'b';
char c2 = 98;
int i2 = 98;
printf("%c %c %c %c\n", c1, i1, c2, i2);
printf("%d %d %d %d\n", c1, i1, c2, i2);
This prints
b b b b
98 98 98 98
You can print an int
, or a char
, using %c
, and you'll get the character with that value.
You can print an int
, or a char
, using %d
, and you'll get a numeric value.
(More on this later.)
in C ++ if I let the compiler choose it will choose (Will give priority to) the char value like in this code
printf("%d", sizeof('b'));
output is:1 (char size) that is meaning the compiler did not change b
to int
What you're seeing is one of the big differences between C and C++: character constants like 'b'
are type int
in C, but type char
in C++.
(Why is it this way? I'll speculate on that later.)
but I can use the int value if I choose like in this code
char x = 'b';
printf("%d", x);
output is 98 (98 is the int which represents b
in the ASCII Code )
Right. And this would work exactly the same in C and C++. (Also my earlier printfs of c1
, i1
, c2
, and i2
would work exactly the same in C and C++.)
So why are the types of character constants different? I'm not sure, but I believe it's like this:
C likes to promote everything to int
. (Incidentally, that's why we were able to pass characters straight to printf
and print them using %d
: the characters all get promoted to int
before being passed to printf
, so it works just fine.) So there would be no point having character constants of type char
, because any time you used one, for anything, it would get promoted to int
. So character constants might as well start out being int
.
In C++, on the other hand, the type of things matters more. And you might have two overloaded functions, f(int)
and f(char)
, and if you called f('b')
, clearly you want the version of f()
called that accepts a char
. So in C++ there was a reason, a good reason, to have character constants be type char
, just like it looks like they are.
Addendum:
The fundamental issue that you're asking about here, that we've been kind of dancing around in this answer and these comments, is that in C (as in most languages) there are several forms of constant, that let you write constants in forms that are convenient and meaningful to you. For any different form of constant you can write, there are several things of interest, but most importantly what is the actual value? and what is the type?.
It may be easier to show this by example. Here is a rather large number of ways of representing the constant value 98 in a C or C++ program:
Form of constant |
base |
type |
value |
98 |
10 |
int |
98 |
0142 |
8 |
unsigned |
98 |
0x62 |
16 |
unsigned |
98 |
98. |
10 |
double |
98 |
9.8e1 |
10 |
double |
98 |
98.f |
10 |
float |
98 |
9.8e1f |
10 |
float |
98 |
98L |
10 |
long |
98 |
98U |
10 |
unsigned |
98 |
'b' |
ASCII |
int/char |
98 |
This table is not even complete; there are more ways than these to write constants in C and C++.
But with one exception, every row in this table is equally true of both C and C++, except the last row. In C, the type of a character constant is int
. In C++, the type of a character constant is char
.
The type of a constant determines what happens when a constant appears in a larger expression. In that respect the type of a constant functions analogously to the type of a variable. If I write
int a = 1, b = 3;
double c = a / b;
it doesn't work right, because the rule in C is that when you divide an int by an int, you get truncating integer division. But the point is that the type of an operand directly determines the meaning of an expression. So the type of a constant becomes very interesting, too, as seen by the different behavior of these two lines:
double c2 = 1 / 3;
double c3 = 1. / 3;
Similarly, it can make a difference whether a constant has type int
or type char
. An expression that depends on whether a character constant has type int
or type char
will behave slightly differently in C versus C++. (In practice, pretty much the only difference that can be easily seen concerns sizeof
.)
For completeness, it may be useful to look at the several other forms of character constant, in the same framework:
Form of constant |
base |
type |
value |
'b' |
ASCII |
int/char |
98 |
'\142' |
8 |
int/char |
98 |
'\x62' |
16 |
int/char |
98 |