C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

C Program: Find ASCII Value of a Character

Introduction

Every character in C (letters, digits, symbols) is internally represented by a unique integer value known as the ASCII (American Standard Code for Information Interchange) value.

For example:

  • 'A' → ASCII value is 65
  • 'a' → ASCII value is 97
  • '0' → ASCII value is 48

In this program, we input a character from the user and print its ASCII value. This demonstrates how characters and integers are related in C, since characters are stored as integers internally.

 

C Program: Find ASCII Value of a Character

C

#include <stdio.h>   // Standard I/O header

 

int main() {

    char ch;

 

    // Input from user

    printf("Enter a character: ");

    scanf("%c", &ch);

 

    // Display ASCII value

    printf("The ASCII value of '%c' is %d\n", ch, ch);

 

    return 0; // Successful termination

}

Output

 
OUTPUT 1 :
Enter a character: A
The ASCII value of 'A' is 65

OUTPUT 2 :
Enter a character: #
The ASCII value of '#' is 35

Explanation:

  1. char ch;
    • Declares a character variable ch to store user input.
  2. scanf("%c", &ch);
    • Reads a character from the user.
  3. printf("The ASCII value of '%c' is %d\n", ch, ch);
    • %c → prints the character.
    • %d → prints its ASCII value (since characters are internally stored as integers).
  4. return 0;
    • Exits the program successfully.