cout << 'a'+'b';
is c++ ??
Main Topics
Browse All Topicswhy printing:
cout << 'a'+char(1);
or
cout << 'a'+'b';
gives you an integer.
shouldn't it me a char type ?
Yair
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
one other thing... if you were wishing to cast 'a'+1 as a char then use this:
cout << char ('a'+1) << endl; the method of casting you used in your sample code will not perform the proper cast if you were looking to get the next character. The output of what I just gave you will be: "b"
does this help?
Simple--a single character delimited by single quotes is always treated by C (and C++) as an integer, even though it's referred to as a character. If you want a single character to be treated as a string, you have to enclose it in double quotes, so
cout << "a" + "b";
would give "ab", which presumably what you wanted.
>Simple--a single character delimited by single quotes is >always treated by C (and C++) as an integer, even though >it's referred to as a character. If you want a single >character to be treated as a string, you have to enclose >it in double quotes, so
>cout << "a" + "b";
>would give "ab", which presumably what you wanted.
so why:
cout << 'a' // prints 'a'
I can't see a simple rule here.
maybe something like
"arithmentic operation with char convert it to int"
we you all agree ?
if you want the interger value of 'a' + 'b' then use:
cout << 'a' + 'b';
if you want the char value of 'a' + 'b' use the following:
cout << char ('a' + 'b'); // this will give you the cumlative value in ASCII which in the case of 'a' + 'b' will be a non-recognizable character. if you want the interger value of 'a' + 'b' just do as the first example shows.
no rule is same but at the time of manipulating char behave like int.
char a = 'a' it actually have 97
char b = 'b' it actually have 98
if a + b it actually like 97 + 98 ->> 195.
if u print that value using cout it give 195.
bcoz the cost of (a + b) is int default ratherthan char.
the cout take it as int.
char c = a + b;
the c store 195.
then u cout << c . cout take cost of c is char and it print char value of 195.
it also apply for short ...
Business Accounts
Answer for Membership
by: fridomPosted on 2002-11-03 at 23:38:11ID: 7404595
This is not a C question but a C++ question.
Regards
Friedrich