c - how can u assign an integer value to a char datatype? -
in below code how can char
data type assigned integer variable (as in char data type alphabets written in single quote) , while printing char data type using %d subscript instead of using %c ?
#include <stdio.h> int main() { char = 30, b = 40, c = 10; char d = (a * b) / c;//a char value storing integer value ?how ? printf ("%d ", d); return 0; }
how can char data type storing integer value
char
s small integers can hold values in range [-128;127]
.
(on systems may [0;255]
. on rare platforms can have size, never see these cases.)
character literals '0'
codes of corresponding symbols. example, '0'
equal 48
.
(on encodings. values rare.)
why %d
works char
? because when pass char
s , short
s ...
function argument, automatically converted ints.
, printf
declared printf(const char *, ...);
.