C Program

Object:-Write a Program to Print ASCII Value

#include <stdio.h>
int main()
{
    char c;
    printf("Enter a character: ");
    // Reads character input from the user
    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: F
ASCII value of G = 70
 

Explanation

In this program, user is asked to enter a character which is stored in variable c. The ASCII value of that character is stored in variable c rather than that variable itself.
When %d format string is used, 70 (ASCII value of 'F') is displayed.
When %c format string is used, 'F' itself is displayed.






You may also like: