1

In the below code how can a char data type be assigned to an integer variable (as in char data type alphabets are written in single quote) and while printing the char data type it is using %d subscript instead of using %c ?

#include <stdio.h> 
    int main()
    {
        char a = 30, b = 40, c = 10;
        char d = (a * b) / c;//a char value is storing integer value ?how ?
        printf ("%d ", d); 
        return 0;
    }

how can a char data type is storing integer value

Cœur
  • 37,241
  • 25
  • 195
  • 267
sagar patel
  • 31
  • 1
  • 5
  • When you store an `int` into a `char`, it just chops off all but the low 8 bits. – Lee Daniel Crocker Apr 09 '15 at 19:58
  • Because, char are data types which hold 1 byte (8 bits) of data. So in char you can store integer values that can be represented by eight bits. That is 0-255 in normal case. So you can store values in that range in char. How ever if you try to store a value out side that range (say 258) then u may have issues. Try it. – sps Apr 09 '15 at 20:16

2 Answers2

3

chars are just small integers that can hold values in range [-128;127].
(On some systems it may be [0;255]. On very rare platforms it can have another size, but you probably will never see these cases.)

Character literals like '0' are just codes of corresponding symbols. For example, '0' is equal to 48.
(On most encodings. Another values are very rare.)


Why %d works for char? Because when you pass chars and shorts to ... function argument, they are automatically converted to ints.
And printf is declared as printf(const char *, ...);.

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
  • 1
    [`char` being signed or unsigned is an implementation decision](http://stackoverflow.com/questions/2054939/is-char-signed-or-unsigned-by-default). [`CHAR_BIT` is usually 8, but not guaranteed](http://stackoverflow.com/questions/2098149/what-platforms-have-something-other-than-8-bit-char). [`'0' == 48` in ASCII and some other encodings, but not all encodings](https://en.wikipedia.org/wiki/EBCDIC). – Bill Lynch Apr 09 '15 at 19:58
  • @BillLynch I intentionally simplified my answer for OP. Ok, I'll add this information. – HolyBlackCat Apr 09 '15 at 20:01
  • The main offender is the signed or unsigned of `char`. That can easily vary over platforms that regular folks may deal with. The others are far less common. – Bill Lynch Apr 09 '15 at 20:02
  • @BillLynch: True, platforms with plain `char` being unsigned are much more common than systems with `CHAR_BIT > 8`. It's also worth noting that all members of the basic character set are required to have non-negative values; a `char` might or might not be able to store a negative value, but `'a'` is always positive. – Keith Thompson Apr 09 '15 at 20:30
0

Additionally, the calculation 30 * 40 does not overflow, because the operands are promoted to int type and converted back to char after the division.

When passed to a variadic function like printf the char is again promoted to int.

Aside: for the float type, that too is promoted when passed to printf, to double. This is why the printf format specifier %f works for both float and double. As for calculations, it is an implementation decision whether or not to promote float to double.

Weather Vane
  • 33,872
  • 7
  • 36
  • 56