Addition of two chars produces int
What you're seeing is the result of the so-called "usual arithmetic conversions" that occur during arithmetic expressions, particularly those that are binary in nature (take two arguments).
This is described in §5/9:
Many 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:
— If either operand is of type
long double
, the other shall be converted tolong double
.
— Otherwise, if either operand isdouble
, the other shall be converted todouble
.
— Otherwise, if either operand isfloat
, the other shall be converted tofloat
.
— Otherwise, the integral promotions (4.5) shall be performed on both operands.54)
— Then, if either operand isunsigned long
the other shall be converted tounsigned long
.
— Otherwise, if one operand is along int
and the otherunsigned int
, then if along int
can represent all the values of anunsigned int
, theunsigned int
shall be converted to along int
; otherwise both operands shall be converted tounsigned long
.
int
— Otherwise, if either operand islong
, the other shall be converted tolong
.
— Otherwise, if either operand isunsigned
, the other shall be converted tounsigned
.[Note: otherwise, the only remaining case is that both operands are
int
]
The promotions alluded to in §4.5 are:
1 An rvalue of type
char
,signed char
,unsigned char
,short int
, orunsigned short int
can be converted to an rvalue of typeint
ifint
can represent all the values of the source type; otherwise, the source rvalue can be converted to an rvalue of typeunsigned int
.2 An rvalue of type
wchar_t
(3.9.1) or an enumeration type (7.2) can be converted to an rvalue of the first of the following types that can represent all the values of its underlying type:int
,unsigned int
,long
, orunsigned long
.3 An rvalue for an integral bit-field (9.6) can be converted to an rvalue of type
int
ifint
can represent all the values of the bit-field; otherwise, it can be converted tounsigned int
ifunsigned int
can represent all the values of the bit-field. If the bit-field is larger yet, no integral promotion applies to it. If the bit-field has an enumerated type, it is treated as any other value of that type for promotion purposes.4 An rvalue of type
bool
can be converted to an rvalue of typeint
, withfalse
becoming zero andtrue
becomingone
.5 These conversions are called integral promotions.
From here, sections such as "Multiplicative operators" or "Additive operators" all have the phrase: "The usual arithmetic conversions are performed..." to specify the type of the expression.
In other words, when you do integral arithmetic the type is determined with the categories above. In your case, the promotion is covered by §4.5/1 and the type of the expressions are int
.
In Java, is the result of the addition of two chars an int or a char?
The result of adding Java chars, shorts, or bytes is an int:
Java Language Specification on Binary Numeric Promotion:
- If any of the operands is of a reference type, unboxing conversion
(§5.1.8) is performed. Then:- If either operand is of type double, the
other is converted to double.- Otherwise, if either operand is of type
float, the other is converted to float.- Otherwise, if either operand
is of type long, the other is converted to long.- Otherwise, both
operands are converted to type int.
But note what it says about compound assignment operators (like +=):
The result of the binary operation is converted to the type of the left-hand variable ... and the result of the conversion is stored into the variable.
For example:
char x = 1, y = 2;
x = x + y; // compile error: "possible loss of precision (found int, required char)"
x = (char)(x + y); // explicit cast back to char; OK
x += y; // compound operation-assignment; also OK
One way you can find out the type of the result, in general, is to cast it to an Object and ask it what class it is:
System.out.println(((Object)('a' + 'b')).getClass());
// outputs: class java.lang.Integer
If you're interested in performance, note that the Java bytecode doesn't even have dedicated instructions for arithmetic with the smaller data types. For example, for adding, there are instructions iadd
(for ints), ladd
(for longs), fadd
(for floats), dadd
(for doubles), and that's it. To simulate x += y
with the smaller types, the compiler will use iadd
and then zero the upper bytes of the int using an instruction like i2c
("int to char"). If the native CPU has dedicated instructions for 1-byte or 2-byte data, it's up to the Java virtual machine to optimize for that at run time.
If you want to concatenate characters as a String rather than interpreting them as a numeric type, there are lots of ways to do that. The easiest is adding an empty String to the expression, because adding a char and a String results in a String. All of these expressions result in the String "ab"
:
'a' + "" + 'b'
"" + 'a' + 'b'
(this works because"" + 'a'
is evaluated first; if the""
were at the end instead you would get"195"
)new String(new char[] { 'a', 'b' })
new StringBuilder().append('a').append('b').toString()
String.format("%c%c", 'a', 'b')
Sum of two chars in C/C++
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.
Why does char concatenation returns int sum?
The difference is in the way the concatenations are constructed.
First: res += str.charAt(0) + str.charAt(2);
Here, the two char
values are added together first. Binary numeric promotion occurs (JLS, Section 5.6.2).
Widening primitive conversion (§5.1.2) is applied to convert either or both operands as specified by the following rules:
If either operand is of type double, the other is converted to double.
Otherwise, if either operand is of type float, the other is converted to float.
Otherwise, if either operand is of type long, the other is converted to long.
Otherwise, both operands are converted to type int.
That means that the values are promoted to int
, creating your 196
. That is then added to str
, appending "196"
.
Second: res = res + str.charAt(0) + str.charAt(2);
Here, the res + str.charAt(0)
is performed first, and a String
plus a char
appends the char
(via String Conversion, JLS 15.18.1, resulting in a new String
.
If only one operand expression is of type String, then string conversion (§5.1.11) is performed on the other operand to produce a string at run time.
Then, the second char
is appended similarly.
If you were to say
res = res + (str.charAt(0) + str.charAt(2));
then the result would be the same (appending the 196
) as with +=
.
Adding two characters and assigning the result to an int in C
char
is sometimes an unsigned type. Use signed char
or unsigned char
if you need to be explicit about it. Adding a print to your program and compiling under both circumstances shows your behaviour:
$ make example && ./example
cc example.c -o example
-11
vs:
$ CFLAGS=-funsigned-char make example && ./example
cc -funsigned-char example.c -o example
501
What is the result of adding a char and an int?
When you add a char
to an int
, the (p)r-value created is promoted to an int
. Therefore what is printed is the int
equivalent to the sum of the (usually) ASCII value + the int.
The ASCII value of 'b' is 'b' == 98
. Therefore 98 + 5 == 103 (integer)
.
Addition of two character literals in c++
With ASCII encoding the values of '9'
and '5'
is 57
and 53
(respectively).
57 + 53
is equal to 110
.
What you're adding is the encoded values of the characters, not their digits.
And you get the output 110
(instead of the ASCII character 'n'
which have the value 110
) because the addition causes the characters to be promoted to int
values and the result is an int
value that is not converted to a char
.
Adding char and int
You're getting that because it's adding the ASCII value of the char. You must convert it to an int first.
Related Topics
C++ Function Pointer (Class Member) to Non-Static Member Function
Relative Performance of Std::Vector VS. Std::List VS. Std::Slist
Iterating C++ Vector from the End to the Beginning
Performance of Qsort VS Std::Sort
Using C Libraries for C++ Programs
Evaluate a String with a Switch in C++
Brace-Enclosed Initializer List Constructor
How Can a C++ Header File Include Implementation
Initialize Parent's Protected Members with Initialization List (C++)
Simple Object Detection Using Opencv and MAChine Learning
When and How to Use Gcc's Stack Protection Feature
How to Initialize C++ Object Member Variables in the Constructor
Is There Any Case Where a Return of a Rvalue Reference (&&) Is Useful
Structure of a C++ Object in Memory VS a Struct
When Is an Object "Out of Scope"
How to Use Profile Guided Optimizations in G++