Addition of two chars produces int

后端 未结 3 1028
遥遥无期
遥遥无期 2020-11-27 06:49

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;
}


        
相关标签:
3条回答
  • 2020-11-27 07:04

    when you are adding these two characters with each other they are first being promoted to int.

    The result of an addition is an rvalue which is implicitly promoted to type int if necessary, and if an int can contain the resulting value. This is true on any platform where sizeof(int) > sizeof(char). But beware of the fact that char might be treated as signed char by your compiler.

    These links can be of further help - wiki and securecoding

    0 讨论(0)
  • 2020-11-27 07:23

    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

    0 讨论(0)
  • 2020-11-27 07:26

    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 is double, the other shall be converted to double.
    — Otherwise, if either operand is float, the other shall be converted to float.
    — Otherwise, the integral promotions (4.5) shall be performed on both operands.54)
    — Then, if either operand is unsigned long the other shall be converted to unsigned long.
    — Otherwise, if one operand is a long int and the other unsigned int, then if a long int can represent all the values of an unsigned int, the unsigned int shall be converted to a long int; otherwise both operands shall be converted to unsigned long int.
    — Otherwise, if either operand is long, the other shall be converted to long.
    — Otherwise, if either operand is unsigned, the other shall be converted to unsigned.

    [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, or unsigned short intcan be converted to an rvalue of type int if int can represent all the values of the source type; otherwise, the source rvalue can be converted to an rvalue of type unsigned 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, or unsigned long.

    3 An rvalue for an integral bit-field (9.6) can be converted to an rvalue of type int if int can represent all the values of the bit-field; otherwise, it can be converted to unsigned int if unsigned 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 type int, with false becoming zero and true becoming one.

    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.

    0 讨论(0)
提交回复
热议问题