Addition of two chars produces int

后端 未结 3 1058
遥遥无期
遥遥无期 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

提交回复
热议问题