Skip to content
Home » Programming » C Examples » C Program to Find ASCII Value of a Character & Vice Versa

C Program to Find ASCII Value of a Character & Vice Versa

C program to find ASCII value of a character

In this post, we will find the ASCII value of a character in C. As well as we will be finding the character equivalent of the ASCII value (or converting ASCII value to character).

ASCII stands for American Standard Code for Information Interchange. ASCII code represents English characters in numbers. There are a total of 128 characters represented by ASCII ( from 0 to 127).

In fact, characters are represented using ASCII value in C programming language.

For example;
     'C' has an ASCII value of 67,
     '+' has an ASCII value of 43,
     and so on.

In C, the characters are not stored directly but their ASCII value is stored. That means whenever you input a value you store its ASCII value rather than the character itself. Let’s say you entered “C”, here you are storing 67 rather than ‘C’ itself.

By using this theory we will find  the ASCII value of character using the C program.

Program to find ASCII Value of a Character in C

#include<stdio.h>

int main() {
     char c;
     int ascii;
     printf("Enter a character: ");
     scanf("%c", &c);

     // %d prints the integer value of character i.e. ASCII value in this case
     // %c prints the character 
     printf("\nWithout Assigning Value:");
     printf("\nASCII value of %c is %d.", c, c);

     // You can also assign the character to integer
     // This assigns the ASCII value of character to the variable
     ascii = c;
     printf("\nWith Assigned the Value:");
     printf("\n ASCII value of %c is %d.", c, ascii);

     return 0;
}

Output

Enter a character: Y

Without Assigning Value:
ASCII value of Y is 89

With Assigning Value:
ASCII value of Y is 89

Explanation

  • Here, we have declared two variables c and ascii.
char c;
int ascii;

We will use the variable c to take input and the variable ascii is used for other purposes we will discuss later.

  • Then we will take input in c.
printf("Enter a character: ");
scanf("%c", &c);
  • Now comes the main part. Here we will display the ASCII value of the character.
printf("\nASCII value of %c is %d.", c, c);

We know that, for printing the character we use %c. You can see that we are doing the same here but also we are using the %d format specifier.

%d displays the ASCII value of the character since the ascii value (which is an integer) is stored in the variable c when we store a character.

- When %d is used with the character variable c, it's ASCII value is displayed.
- And when %c is used the character itself is displayed.
  • Going forward, we also wanted to check what happens when we assign a character to integer variables.
    You might have guessed it; ASCII value gets assigned to the integer variable.
    This is the reason why we got 89 when we printed the ascii variable.

Can we convert ASCII to Character?

Yes, we can. When we assign a ASCII value (which is an integer) to a variable of character data type we will get a character in that variable. The value assigned should be greater than or equal to 0 and smaller than 128 since the ASCII value ranges from 0 to 127.

Also, if we print an integer (in the range of ASCII values) using a %c format specifier a character will be printed.

Program to find ASCII to Character in C

#include <stdio.h>

int main() {
     char c;
     int k = 97;

     c = 65;
     printf("%c", c);
     printf("\n%c", k);

     return 0;  
}

Output

A
a

Explanation

  • For checking the assignment of an integer in the variable c which is a variable of data type char,
c = 65;
printf("%c", c);

Here, you can see that we have assigned an integer value 65 in the variable c. So, when we print it using the %c format specifier “A” is printed which is the character equivalent of the ASCII value 65.

  • Likewise, if we print an integer variable with %c format specifier a character will be printed equivalent to ASCII value provided.
printf("\n%c", k);

We had initialized the variable k with the value 97. Thus, when we print it with %c equivalent character is printed i.e a.

Program to find ASCII Value of all Characters C

#include <stdio.h>

int main() {
     // variable declaration
     int i;

     // for loop from 0 to 127
     for(i = 0; i < 128; i++) {
          printf("\nThe ascii value of %c is %d.", i, i);
     }

     return 0;  
}

Output is not shown here since printing all 128 lines for all characters will be lengthy. You can try it on your own for this.

Explanation

  • To display ASCII values of all characters we will use for loop.
for(i = 0; i < 128; i++) {
     . . .
}

Loop iterates from 0 to 127 since we have 128 ASCII values which range from 0 to 127.

  • And for printing,
printf("\nThe ascii value of %c is %d.", i, i);

This is the same as printing for a single character as we have done before in this post.

You can also check other Example Programs

Leave a Reply

Your email address will not be published. Required fields are marked *