How to convert int to char in C?
Hi, this article will talk about How to convert int to char in C?
One simple way for converting int to char in C is to add the integer to character zero('0'). The idea behind this is that '0' will be converted to its ASCII, which is 48.
So internally it works something like:
char ch = (char)(1 + 48);
char ch = (char)49;
char ch = '1';
Checkout the below code :
#include <stdio.h>
int main()
{
int i = 1;
char ch = i + '0';
printf("Integer - %d is converted to Character - %c \n",i,ch);
return 0;
}
Output :
Integer - 1 is converted to Character 1

0 Comments