casting an int to a char in c
In C, you can cast an int
to a char
by using the cast operator. Here is an example:
int num = 65;
char ch = (char)num;
In this example, the int
variable num
is assigned the value 65
. By using the cast operator (char)
, we convert the int
value to a char
value and assign it to the char
variable ch
. The resulting value of ch
will be the ASCII character corresponding to the decimal value 65
, which is the capital letter 'A'
.
Note that when casting an int
to a char
, the value may be truncated if it is too large to fit into a char
.