code as follows:
#include <stdio.h>
int main(){
char a = 1;
char b = 2;
printf("%d %d\n", a, b);
}
output: 1 2
code as follows:
#include <stdio.h>
int main(){
char a = 1;
char b = 2;
printf("%d %d\n", a, b);
}
output: 1 2
total number of Character in ASCII table is 256 (0 to 255), in C language each character represented using the integer value, interesting thing is that if you use %d the it will print the integer code for the character
printf("%d", 'a'); /* output : 97 /*
but if you use %c the it will print the character
printf("%c", 'a'); /* output : a */
printf("%c", 97); /* output : a */
char a=1 ;/* this a is not storing 1 but it is holding the ascii character
correspond to 1, this is due to implicit type casting done by compiler */
refer to this :https://www.geeksforgeeks.org/type-conversion-c/
The char
type is an integer type, similar to short
or int
. When you store characters in a char
object, it actually stores a numeric code in the char
object. For example, x = 'b';
stores a code for “b” in x
. (In implementations that use ASCII, the code for “b” is 98.) If you read a character from input and store it with x = getchar();
, and the input is “b”, then the code for “b” is stored.
In many expressions in C, a char
value is automatically promoted to an int
. This includes when it is passed to printf
. If you print the value using %c
, printf
prints the character represented by the value. If you print using %d
, printf
prints a decimal numeral for the value.
char
may be a signed type, in which case it can represent values at least from −127 to +127. Or it may be unsigned, in which case it can represent values from 0 to at least 255.
Chars will store integers in the range of -128 to 127 or from 0 to 255 depending if the char is signed or unsigned. The printf function arguments a
and b
are promoted to ints, and printed as ints as expected by the %d
delimiter. With the delimiter %c
they are still promoted to ints, but printed as characters.