C Program to Convert int to char with Example
In C, you can convert an int to a char by using simple casting like
char c = (char)intValue; or by adding '0' for digit characters like char c = intValue + '0';.Examples
InputintValue = 5
Outputchar c = '5'
InputintValue = 65
Outputchar c = 'A'
InputintValue = 0
Outputchar c = '0'
How to Think About It
To convert an integer to a character in C, first decide if the integer represents a digit (0-9) or an ASCII code. For digits, add the character '0' to get the correct character. For ASCII codes, cast the integer directly to char.
Algorithm
1
Get the integer value to convert.2
If the integer is a digit (0-9), add '0' to convert it to the corresponding character.3
Otherwise, cast the integer directly to a char.4
Store the result in a char variable.5
Print or return the char value.Code
c
#include <stdio.h> int main() { int num = 5; char c = num + '0'; printf("The char representation of %d is %c\n", num, c); return 0; }
Output
The char representation of 5 is 5
Dry Run
Let's trace the example where num = 5 through the code.
1
Initialize integer
num = 5
2
Convert int to char
c = num + '0' = 5 + 48 = 53 (ASCII for '5')
3
Print result
Output: The char representation of 5 is 5
| Step | num | Operation | Result |
|---|---|---|---|
| 1 | 5 | Initialize num | 5 |
| 2 | 5 | c = num + '0' | '5' (ASCII 53) |
| 3 | 5 | Print c | '5' |
Why This Works
Step 1: Why add '0' to the integer?
Adding '0' (ASCII 48) to a digit integer converts it to the correct character digit because characters for digits are sequential in ASCII.
Step 2: Casting int to char
Casting an int directly to char converts the integer to the character with that ASCII code.
Step 3: Printing the character
Using %c in printf prints the character representation of the stored char variable.
Alternative Approaches
Direct cast for ASCII codes
c
#include <stdio.h> int main() { int num = 65; // ASCII for 'A' char c = (char)num; printf("The char for ASCII %d is %c\n", num, c); return 0; }
Use this when the int represents an ASCII code, not just digits.
Using sprintf to convert int to char string
c
#include <stdio.h> int main() { int num = 5; char str[2]; sprintf(str, "%d", num); printf("The char string is %s\n", str); return 0; }
This converts int to a string, useful if you want a char array instead of a single char.
Complexity: O(1) time, O(1) space
Time Complexity
The conversion is a simple arithmetic or cast operation, so it runs in constant time.
Space Complexity
Only a single char variable is used, so space is constant.
Which Approach is Fastest?
Adding '0' or casting are both constant time and space; using sprintf is slower and uses more memory.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Add '0' to digit | O(1) | O(1) | Converting digit ints to char digits |
| Direct cast | O(1) | O(1) | Converting ASCII code ints to chars |
| sprintf to string | O(n) | O(n) | Converting int to char string |
Add '0' to an integer digit to get its character form in C.
Trying to assign an int directly to a char without casting or adding '0' can give unexpected characters.