ASCII Value of a Character in C

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

Every character has an ASCII value associated with it. Characters are stored in the memory using their ASCII values in C. If we wish to know the ASCII value of any given character, we can write a program to find out the ASCII values in C.

Prerequisites

C Program To Print ASCII Value of a Character​​

To display the ASCII values in C of any character variable, we use the %d format specifier in the printf() statement. Let us take a look at how it works.

Input:

Output:

In the above example, we printed the ASCII value in C of the letter "z" by using %d format specifier in the printf() statement. The %d specifier returns an integer value. So, when we use it in a printf() statement, the character variable automatically gets converted to its corresponding ASCII value.

Complexity Analysis

  • Time Complexity : O(1)

    • The time complexity of this program is constant. Regardless of the input character, the program's execution time remains the same. It directly prints the ASCII value using the %d format specifier, which is an efficient operation with a constant time overhead.
  • Auxiliary Space Complexity : O(1)

    • The program's space complexity is also constant. It utilizes a minimal amount of additional memory for storing variables and the input character. The space required does not depend on the size of the input or any other factors, making it O(1).

This program's efficiency in terms of time and space complexity makes it a suitable choice for quickly obtaining the ASCII value of a character without any significant resource overhead.

Conclusion

  • ASCII stands for American Standard Code for Information Interchange.
  • There are 256 ASCII encoded characters but we use only 128 characters (0 to 127).
  • We can print the ASCII values in C of any character by using the %d format specifier.
  • To print all ASCII characters, we can use a loop that iterates through numbers 0 to 255 and then print the characters using %c and %d format specifiers.