Search code examples
ccharacter

Operations with Characters and Integers in C


I am new in programming Language and I need your help here. I am here studying someone's code and I can across with these expressions, my doubt is how is the operation done here, given that a character and an integers are two different data types? How will the integer type hold the character value?

Thanks

int line, col;
char ch;
scanf("%d%c", &line, &ch);
//line--;
col = ch - 'A';

Solution

  • When you subtract two characters and put them in a variable of integer type, in fact the ASCII code of the two characters is subtracted. For example when you have:

    int col = 'D' - 'A';
    

    The value of col is equal to 3 Because ascii code of D is equal to 68 and ascii code of A is 65. So col is 3, however D & A were character. Also you can see here