Convert Integer to Char in C
-
Add
'0'
to Convert anint
tochar
-
Assign an
int
Value tochar
Value -
sprintf()
Function to Convert an Int to a Char

This tutorial introduces how to convert an integer value into a character value in C. Each character has an ASCII code, so it’s already a number in C. If you want to convert an integer to a character, simply add '0'
.
Add '0'
to Convert an int
to char
The '0'
has an ASCII value of 48. so, we have to add its value to the integer value to convert it into the desired character. The program is as below:
#include<stdio.h>
int main(void)
{
int number=71;
char charValue = number+'0';
printf("The character value is :%c",charValue);
return 0;
}
Output:
The character value is: w
Below is a program that will convert an integer to the character only between 0 to 9.
#include<stdio.h>
int main(void)
{
int number=7;
char charValue=number+'0';
printf("The value is :%c",charValue);
return 0;
}
Output:
The value is: 7
Another program to convert an integer value to a character is as below:
#include<stdio.h>
int main(void)
{
char charValue[] = "stringValueX";
int anyNumber;
for (anyNumber= 0; anyNumber< 10; ++anyNumber)
{
charValue[11] = anyNumber+ '0';
puts(charValue);
}
return 0;
}
Output:
stringValue0
stringValue1
stringValue2
stringValue3
stringValue4
stringValue5
stringValue6
stringValue7
stringValue8
stringValue9
Assign an int
Value to char
Value
Another way to convert an integer value to a character value is to assign an integer value to a character value as below: The character value corresponding to an integer value is printed here.
#include<stdio.h>
int main(void)
{
int number = 65;
char charvalue = number;
printf("The character value :%c", charvalue);
return 0;
}
Output:
The character value : A
Below is another way to convert an integer value to a character value. Here, the value is typecasted, so the value 67 gets converted into the corresponding ASCII value.
#include<stdio.h>
int main(void)
{
int number = 67;
char charValue = (char)number;
printf("The character value = %c", charValue);
return 0;
}
Output:
The character value = C
sprintf()
Function to Convert an Int to a Char
The sprintf()
function works the same as the printf()
function but instead of sending output to console, it returns the formatted string.
The first argument to the sprintf()
function is a pointer to the target string. The remaining arguments are the same as for the printf()
function.
Syntax of sprintf()
int sprintf(char *strValue, const char *format, [arg1, arg2, ... ]);
strValue
is a pointer to the char data type.format
is used to display the type of output along with the placeholder.[arg1,arg2...]
are the integer(s) to be converted.
The function writes the data in the string pointed to by strValue
and returns the number of characters written to strValue
, excluding the null character. The return value is generally discarded. If any error occurs during the operation, it returns -1
. The program to convert an integer to a character is as below:
#include<stdio.h>
int main(void)
{
int number = 72;
char charValue[1];
sprintf(charValue, "%c", number);
printf("The character value = %s", charValue);
return 0;
}
Output:
The character value = H