I've made a simple program and compiled it with GCC 4.4/4.5 as follows:
int main () { char u = 10; char x = 'x'; char i = u + x; return 0; } g++ -c -Wconversion a.cpp
And I've got the following:
a.cpp: In function ‘int main()’: a.cpp:5:16: warning: conversion to ‘char’ from ‘int’ may alter its value The same warning I've got for the following code:
unsigned short u = 10; unsigned short x = 0; unsigned short i = u + x; a.cpp: In function ‘int main()’: a.cpp:5:16: warning: conversion to ‘short unsigned int’ from ‘int’ may alter its value Could anyone please explain me why addition of two chars (or two unsigned shorts) produces int? Is it a compiler bug or is it standard compliant?
Thanks.
The easiest is adding an empty String to the expression, because adding a char and a String results in a String.
In Java, char can be converted to int value using the following methods: Implicit type casting ( getting ASCII values ) Character. getNumericValue()
In C, the strcat() function is used to concatenate two strings. It concatenates one string (the source) to the end of another string (the destination). The pointer of the source string is appended to the end of the destination string, thus concatenating both strings.
Example: One can add character at the start of String using the '+' operator.
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 longthe other shall be converted tounsigned long.
— Otherwise, if one operand is along intand the otherunsigned int, then if along intcan represent all the values of anunsigned int, theunsigned intshall 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 intcan be converted to an rvalue of typeintifintcan 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
intifintcan represent all the values of the bit-field; otherwise, it can be converted tounsigned intifunsigned intcan 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
boolcan be converted to an rvalue of typeint, withfalsebecoming zero andtruebecomingone.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.
When you do any arithmetic operation on char type, the result it returns is of int type.
See this:
char c = 'A'; cout << sizeof(c) << endl; cout << sizeof(+c) << endl; cout << sizeof(-c) << endl; cout << sizeof(c-c) << endl; cout << sizeof(c+c) << endl; Output:
1
4
4
4
4
Demonstration at ideone : http://www.ideone.com/jNTMm
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With