C Program to find out the ASCII value of a character

In this C Programming example, we will implement the program to find out the ASCII value of a character using the user’s input.

1. What is an ASCII Value?

The first question that arises is β€œwhat is an ASCII value?” And the answer is, “ASCII is a short form for American Standard Code for Information. It is a character encoding standard for electronic communication that assigns letters, numbers, special characters, and other characters in the 256 slots which are available in the 8-bit code. It is created from binary numbers.”

For Example 
Input : l
Output : 108

Helpful topics to understand this program better are-


2. C Program to find out the ASCII value of a character

Let’s discuss the execution(kind of pseudocode) for the program to find out the ASCII value of a character in C.

  1. The user enters the character for which we need to find out the ASCII value.
  2. The char is stored in the variable char ch.
  3. When we assign this char to an integer variable it assigns the ASCII value of that character, as we do in int aascii = ch;.

Let us implement this concept in the c program and find out the ASCII value of a character.

#include <stdio.h>
int main() {
  char ch;
  
  printf("Enter the character to get the ASCII value: ");
  scanf("%c", &ch);
  
  int ascii = ch;

  printf("ASCII value of %c = %d", ch, ascii);
  return 0;
}
Enter the character to get the ASCII value: A
ASCII value of A = 65

3. C Program to convert ASCII value to a character

We can also directly print from Character to ASCII number and from ASCII number to character without using another variable. We just have to use %c and %d properly.

#include <stdio.h>
int main() {
  // change to user input if needed
  char ch = 'A'; 
  int ascii = 65; // Ascii value of 'A'
  char from_ascii = ascii;

  printf("ch to Ascii = %d", ch);
  printf("\nascii to char = %c", ascii);
  printf("\nascii to char variable = %c", from_ascii);
  return 0;
}
Output
ch to Ascii = 65
ascii to char = A
ascii to char variable = A

4. Conclusion

In this C Programming example, we have discussed how to find out the ASCII value of a character and how to convert the ASCII value to a character.


Helpful Links

Please follow C Programming tutorials or the menu in the sidebar for the complete tutorial series.

Also for the example C programs please refer to C Programming Examples.

All examples are hosted on Github.

An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding

Do not forget to share and Subscribe.

Happy coding!! 😊

Recommended -

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x