Convert Integer to ASCII Character in C/C++



In C and C++, every character like 'A', 'b', '3', or '@' is stored as a number called its ASCII value. For example, 'A' is 65, and 'a' is 97. Given an integer like 97, we can convert it to its corresponding ASCII character which is 'a'. In this article, we will learn how to write a C and C++ program to convert an integer into its ASCII character.

For example, we're given any integer from 0 to 127 (because the ASCII table contains 128 characters), and we need to convert it into its corresponding ASCII character:

Input: 65 -> Output: 'A'  
Input: 97 -> Output: 'a'  
Input: 57 -> Output: '9'  
Input: 33 -> Output: '!'
Input: 36 -> Output: '$''

We can convert an integer to an ASCII character using simple methods in both C and C++. Here are two common ways to do it.

Typecasting to Convert int to ASCII Character

In this method, we convert an integer directly to a character using typecasting. Since ASCII characters are stored as numbers inside the computer, casting an int to char gives us the character that matches the ASCII value.

C++ Program to Convert int to ASCII Character

Below is a complete C++ example where we convert a number to its ASCII character using typecasting.

#include <iostream>
using namespace std;

int main() {
    // storing the integer
    int asciiValue = 97; 
    // converting integer to ASCII character
    char character = (char)asciiValue;
    cout << "The ASCII character for " << asciiValue << " is '" << character << "'" << endl;  
    return 0;
}

Below is the output of the above program that shows the ASCII character corresponding to the given integer.

The ASCII character for 97 is 'a'

Time Complexity: O(1), because typecasting is constant-time.

Space Complexity: O(1), as only one char variable is used.

C Program to Convert int to ASCII Character

Here's a complete C program that shows how to convert an integer into its ASCII character by typecasting.

#include <stdio.h>

int main() {
    // Given integer, you can modify to check yours
    int asciiValue = 65; 
    // converting integer to ASCII character
    char character = (char)asciiValue; 
    printf("The ASCII character for %d is '%c'\n", asciiValue, character);
    return 0;
}

The output below displays the ASCII character corresponding to the given integer.

The ASCII character for 65 is 'A'

Time Complexity: O(1), because casting is instant.

Space Complexity: O(1) because we use only one variable.

Using printf or cout for int to ASCII Conversion

In this approach, we directly pass the integer to the output function using format specifiers to display the corresponding ASCII character.

C++ Program using cout for int to ASCII conversion

Here's a complete C++ example where we print the ASCII character by casting the integer directly inside the cout statement.

#include <iostream>
using namespace std;

int main() {
    // Integer value for ASCII character
    int asciiValue = 33; 
    // printing the output showing ASCII character
    cout << "The ASCII character for " << asciiValue << " is '" << char(asciiValue) << "'" << endl; 
    return 0;
}

Below is the output that shows the ASCII character of a given integer value.

The ASCII character for 33 is '!'

Time Complexity: O(1).

Space Complexity: O(1).

C Program using printf for int to ASCII conversion

Below is a simple C program where we use printf() to print the ASCII character directly without storing it in a variable.

#include <stdio.h>

int main() {
    // storing the integer value to convert into ascii value
    int asciiValue = 48; 
    printf("The ASCII character for %d is '%c'\n", asciiValue, asciiValue);
    return 0;
}

The output below shows the ASCII character corresponding to the given integer value.

The ASCII character for 48 is '0'

Time Complexity: O(1).

Space Complexity: O(1).

Updated on: 2025-05-30T18:01:36+05:30

36K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements