Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sum of two chars in C/C++

Tags:

c++

c

In C/C++ when I want to find sum of two chars - I get result in int.
For example:

#include <stdio.h>
int main(){
   char a = 'a', b = 'b';
   printf("%d + %d = %d\n", sizeof(a), sizeof(b), sizeof(a + b));
   return 0;
}

Prints

1 + 1 = 4

Why?

like image 958
Mahnerak Avatar asked Oct 19 '13 21:10

Mahnerak


4 Answers

Because although a and b are each of type char, the expression a + b is of type int. Anytime you do math with char types, they are converted to int before doing the actual calculations.

like image 137
Lee Daniel Crocker Avatar answered Oct 09 '22 16:10

Lee Daniel Crocker


1) This just prints out the text string "something + something else": you're not actually adding anything: printf("%d + %d = %d\n",..)

2) sizeof(<some char>) will always be "1". That's what "sizeof()" means - it can never be anything besides "1".

3) Yes, adding type "char" (which is an integer subtype) will give an integral result.

For further details:

http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf

The type char, the signed and unsigned integer types, and the enumerated types are collectively called integer types. The integer and real floating types are collectively called real types.

like image 33
paulsm4 Avatar answered Oct 09 '22 17:10

paulsm4


Because, if not promoted, the result can overflow.

A single byte char cannot hold value greater than 255 (unsigned) or +127 (signed). When you sum two instances, there is always the possibility of overflow i.e. result exceeding 255. This means it cannot be stored in a single byte. Hence int is used which cannot over flow max sum of two chars or bytes.

like image 22
fkl Avatar answered Oct 09 '22 17:10

fkl


Section 4.5 Integral promotions

A prvalue of an integer type other than bool, char16_t, char32_t, or wchar_t whose integer conversion rank (4.13) is less than the rank of int can be converted to a prvalue of type int if int can represent all the values of the source type; otherwise, the source prvalue can be converted to a prvalue of type unsigned int.

The conversion is mandated by the standard in what is called "The usual arithmetic conversions" Clause 5 [expr] point 10:

Many binary operators that expect operands of arithmetic or enumeration type cause conversions and yield result types in a similarMany binary operators that expect operands of arithmetic or enumeration type cause conversions and yield result types in a similar way. The purpose is to yield a common type, which is also the type of the result. This pattern is called the usual arithmetic conversions, which are defined as follows: way. The purpose is to yield a common type, which is also the type of the result. This pattern is called the usual arithmetic conversions, which are defined as follows:

Unless for a few select types. long double, double and float

Otherwise, the integral promotions (4.5) shall be performed on both operands.59

The (char)+(char) results in an int.

Also note that a char+char is tricky. The char can be signed or unsigned depending on implementation; so the result might easily overflow for normal values which is likely why it is not included in the standard as an exception.

like image 4
Captain Giraffe Avatar answered Oct 09 '22 18:10

Captain Giraffe