How to subtract integers from characters in C?
c, c++, char, int, variables
Solution
The output is to be expected. `'0'` is a `char` value that, since your compiler presumably uses the ASCII encoding, has value 48. This is converted to `int` and subtracted from `1`. Which gives the value `-47`.
So the program does what it is expected to do, just not what you might hope it would do. As for what you are doing wrong, it is hard to say. I'm not sure what you expect the program to do, or what problem you are trying to solve.
Problem
Brian Kernighnan in his book Programming with C says By definition, chars are just small integers, so char variables and constants are identical to ints in arithmetic expressions. Does this mean we can subtract char variable from int ?? I wrote a small piece of code: ``` #include <stdio.h> main() { int a ; int c; a = 1; c = 1 - '0' ; printf("%d", c); } ``` However it gives me output = -47... What is that I'm doing wrong ?? Are the variables I assigned have the right type??