C Example for Beginners: C Program to Find ASCII Value of a Character

(C programming Example for Beginners)

C Program to Find ASCII Value of a Character

In this example, you will learn how to find the ASCII value of a character.


In C programming, a character variable holds ASCII value (an integer number between 0 and 127) rather than that character itself. This integer value is the ASCII code of the character.

For example, the ASCII value of 'A' is 65.

What this means is that, if you assign 'A' to a character variable, 65 is stored in the variable rather than 'A' itself.

Now, let’s see how we can print the ASCII value of characters in C programming.


Program to Print ASCII Value

#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: G
ASCII value of G = 71

In this program, the user is asked to enter a character. The character is stored in variable c.

When %d format string is used, 71 (the ASCII value of G) is displayed.

When %c format string is used, 'G' itself is displayed.


Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.