Call us at 0700-922-6559 or Click here

Write a program to Find ASCII Value of a Character

#include <stdio.h>

int main() {  

    char c;

    printf(“Enter a character: “);

    scanf(“%c”, &c);   

    // %d displays the integer value of a character

    // %c displays the actual character

    printf(“ASCII value of %c = %d”, c, c);   

    return 0;

}

Output-

Enter a character: C

ASCII value of C = 67

 let’s break down the C program step by step:

  1. #include <stdio.h>: This line includes the standard input-output library, which provides functions like printf and scanf for input and output operations.
  2. int main(): This line marks the beginning of the main function, which is the entry point of a C program. It returns an integer (int) value to the operating system when the program finishes running. In this case, it takes no arguments, indicated by the empty parentheses ().
  3. {: The curly brace marks the beginning of the main function’s body, where the actual code of the program resides.
  4. char character;: Here, you declare a character variable named character to store the user’s input character.
  5. printf(“Please enter a character: “);: This line uses the printf function to display the message “Please enter a character: ” on the console, prompting the user to input a character.
  6. scanf(“%c”, &character);: This line uses the scanf function to read input from the user. It expects a character input (%c) and stores it in the character variable. The & operator is used to provide the memory address of the character variable so that scanf can store the input character there.
  7. printf(“ASCII value of ‘%c’ = %d\n”, character, character);: Here, you use printf to display the result. It prints the entered character (enclosed in single quotes for clarity), followed by its ASCII value as an integer.
  8. return 0;: This line indicates the end of the main function and returns an exit status of 0 to the operating system, typically indicating successful program execution.

When you run this program:

It prompts you to enter a character.

You input a character (e.g., “C”) and press Enter.

The program calculates and displays the ASCII value of the entered character. For “C,” it displays “ASCII value of ‘C’ = 67.”

To learn more about C Programming join our course.

Leave a Reply

Your email address will not be published. Required fields are marked *