From Letters to Numbers: ASCII Conversion in C

codekrackerz
2 min readAug 20, 2023

--

Thumbnail for “Converting character to ASCII code” story.

Hey there welcome back!! Kodekrackerz here 👋

Today we’re going to see how you could convert a character (so called char datatype in c language) to their ASCII (American Standard Code for International Exchange) values.

The logic behind this code is to accept a character for the user a input and print it in print statement using %d format specifier. As you know if we want to print a character in print statement we usually use %c format specifier as in this use case we need to print the ASCII value of the character instead.

Below is the code for our program and output of it as well.

// C

#include <stdio.h>

void main()
{
char a;

// Accept a character from the user
printf("Enter a character: ");
scanf("%c", &a);

// Print the ASCII value of the character
printf("ASCII value of %c is %d \n", a, a);
}

Enter a charachter: q
ASCII value of q is 113

If you want to understand more about the syntax used in this code check out our old blogs for c programming.

⚠️ Note: Just so you know if you enter more than one input such as “qw” our program only accepts the first character for considering the output hence “113" is printed as the output. Same occurs when you enter a 2 or more digit number as well only the first digit is selected.

Some fun facts about ASCII values

  1. ASCII values are from 0–128 so called 2⁷.
  2. It contains some hidden values such as BEL which was used to ring a bell on olden days.
  3. ASCII laid the groundwork for modern emojis! The smiley face “:-)” was a precursor to the emojis we use today. It’s like the grandparent of 🥰.
  4. The ASCII value of space is 32. It’s like a silent character that gives breathing room between words. Even the space has it’s own value!
  5. The ASCII values for uppercase letters ‘A’ through ‘Z’ are 65 to 90, while lowercase letters ‘a’ through ‘z’ are 97 to 122.

Thanks for reading 😄 See you in the next one 😎 Feel free to comment to this we’ll be happy to respond 🚀.

--

--