Convert an int to ASCII character in C/C++


In C or C++ the character values are stored as ASCII values. To convert int to ASCII we can add the ASCII of character ‘0’ with the integer. Let us see an example to convert int to ASCII values.

Example

#include<stdio.h>
int intToAscii(int number) {
   return '0' + number;
}
main() {
   printf("The ASCII of 5 is %d\n", intToAscii(5));
   printf("The ASCII of 8 is %d\n", intToAscii(8));
}

Output

The ASCII of 5 is 53
The ASCII of 8 is 56

Updated on: 05-Oct-2023

26K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements