• Post author:
  • Reading time:1 mins read
  • Post category:C Programs

In this example, you will learn to convert an int variable to char type variable.

How to convert int to char in c?

We can convert an integer to the character by adding a ‘0’ (zero) character.

The char data type is represented as ascii values in c programming. Ascii values are integer values if we add the ‘0’ then we get the ASCII of that integer digit that can be assigned to a char variable. Then if we access that char using %c we can print it.

If we want to convert a number then we have to extract the digit and then we have to convert it to a char array.

Have a look at the below example for converting an int to a character value.


Example:

#include<stdio.h>
int main()
{
 int a=1;
 char c=a+'0';
 printf("Converted character is %c",c);
}

In the above example, a single-digit int is converted to char type.

Variable a is assigned the value 1.

Variable a is converted to char type by adding '0' and assigned to char type variable c

Output

	Converted character is 1