0

I have a sample code in C below:

#include<stdio.h>
int main(){
    int out = 4 + '4';
    printf("%d",out);
    return 0;
}

When I run it, the output value it return is 56. Can someone explain why?

jophab
  • 5,356
  • 14
  • 41
  • 60
Co May Hoa
  • 33
  • 1
  • 6
  • Visit [ASCII table](http://www.asciitable.com/) `4: 52` in ASCII code representation. when we add 4 to 52 we have 56. all `char` values in `C` cast to correspond integer – EsmaeelE Aug 27 '17 at 06:57
  • I suggest add a new line after last output to screen. `printf("%d \n",out);` – EsmaeelE Aug 27 '17 at 06:59

2 Answers2

3

'4' is a representation of the int value 52. 4 + 52 = 56.

jodag
  • 19,885
  • 5
  • 47
  • 66
1

ASCII value of '4' is 52.

Char value '4' has integer value 52 which is its ASCII Code. This is added to integer value 4.

Thus the result 56

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
jophab
  • 5,356
  • 14
  • 41
  • 60