Monday, March 30, 2020

c program to Find ASCII Value of a Character

C Program to Find ASCII Value of a Character

In C programing, a Character variables holds ASCII value (an integer number between 0 to 127)
rather than that character itself. That value is known as it's ASCII value.
For example, the ASCII value of  'A' is 65.
What this means that, if you assign 'A' to a character variable, 65 is stored in the variable rather than 'A' itself.
Now, how we can print ASCII value of character in C programming. 
#include 
int main()
 {  
    char c; // Character value
    printf("Enter a character: "); // user input 
    scanf("%c", &c);  // Storing value 
    
    // %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: D
ASCII value of G = 68
In this program, the user is asked to entered a character. The character is stored in variable  c.
When %d format string is used, 68( the ASCII value of  D) is displayed.
When  %c format string is used, 'D' itself displayed.

Happy coding

0 comments:

Post a Comment

Recent Posts