✓ Link copied to clipboard
C 📅 July 2, 2026 ⏱ 4 min read

C Program to Find ASCII Value of a Character

C Program to Find ASCII Value of a Character

Prerequisites for the ASCII Value Program in C

Before you start finding the ASCII value of a character using C, ensure you have these prerequisites:

  • Understanding of character data types and variables in C.
  • Familiarity with printf and scanf functions for input and output.
  • Knowledge of the (int) typecasting technique to convert a character to its ASCII integer value.
  • Any C compiler such as GCC 9 or newer — or just run it in the EduSeekho online compiler, here.

Source Code of C Program to Find ASCII Value of a Character

Here's the complete code for finding the ASCII value of a character in C. It reads a character from the user and prints its ASCII value.

ascii_value.cC▶ Run
#include <stdio.h>

int main() {
    char character;
    int ascii_value;

    // Prompt the user for a character
    // Read the character input
    printf("Enter a character: ");
    scanf(" %c", &character);

    // Convert the character to its ASCII value
    ascii_value = (int) character;

    // Print the ASCII value
    printf("ASCII value of %c = %d\n", character, ascii_value);

    return 0;
}

Try this code yourself — free

Run it in the EduSeekho online compiler. No setup needed.

▶ Open Compiler

Output of the Find ASCII Value Program

Run the program and you'll see this output when you enter a character:

OUTPUTtext
Enter a character: A
ASCII value of A = 65
Output of C Program to Find ASCII Value of a Character
Output of C Program to Find ASCII Value of a Character

Algorithm to Find ASCII Value in C

Finding an ASCII value involves reading a character and converting it to its integer equivalent. Here's how the logic unfolds step by step.

  1. Read a character from the user.
  2. Convert the character to its ASCII value using typecasting.
  3. Print the ASCII value.
flowchart TD
  A([Start]) --> B[/Read character/]
  B --> C["ascii_value = (int) character"]
  C --> D[/Print ascii_value/]
  D --> E([End])
Flowchart: the algorithm step by step

How the C Program to Find ASCII Value of a Character Works

Let's break down how the program works line by line, making it easier to understand.

  • #include <stdio.h>: This line includes the standard input-output library, essential for using printf and scanf.
  • int main(): The main function where execution starts.
  • char character;: Declares a variable character to store user input.
  • int ascii_value;: Declares an integer to store the ASCII value of the character.
  • printf("Enter a character: ");: Prompts the user to enter a character.
  • scanf(" %c", &character);: Reads the character input from the user.
  • ascii_value = (int) character;: Converts the character to its corresponding ASCII value.
  • printf("ASCII value of %c = %d\n", character, ascii_value);: Displays the ASCII value of the character.
  • return 0;: Ends the program successfully.
🧠

Quick Quiz — test your understanding

Tap an option to check your answer

What is the ASCII value of the character 'A'?

🎉Correct! The ASCII value of 'A' is 65.
🤔Not quite. Remember, 'A' is an uppercase letter.

Time and Space Complexity of the C Program to Find ASCII Value of a Character

Examining the time and space efficiency of this program helps us understand its performance.

Time Complexity: O(1). The program executes a fixed number of operations, including reading the input and printing the output, regardless of the character entered.

Space Complexity: O(1). The program uses only a few variables: character and ascii_value, occupying a constant amount of memory.

⚠️

Common mistake: Missing the space in the scanf format string can cause unexpected input problems.

Key takeaway: Figuring out ASCII values is key for understanding character encodings.

FAQs About the C Program to Find ASCII Value

Here are some frequently asked questions about this program:

ASCII value is a number that represents a character in the ASCII table.
Typecasting with (int) changes the character to its ASCII integer value.
Yep, the code works for any printable ASCII character.
The program sees it as a character and prints its ASCII value.
Sure, the space in scanf(" %c", &character); makes sure it reads the character right.

If you found this program helpful, check out these related programs:

Conclusion

Getting ASCII values really helps with text processing tasks. This program’s a simple way to convert characters to their number counterparts. It sets you up for more advanced string manipulation later. Try converting whole strings to their ASCII values for more insight.

Test your C skills further with our C programming quiz or run more codes in our online C compiler.