C Program to Find ASCII Value of a Character

by | Jan 2, 2023 | C, C Programs

Home » C » C Programs » C Program to Find ASCII Value of a Character

Introduction

In this program, we will find the ASCII value of a character. This will enable us to find the ASCII value of every character that exists in C programming. The concept of ASCII values is given in the article ‘Storage of a character value’. For more information, you may refer to that article in the C language series.

Moreover, to understand this program, you should know about:

1. Data types in C

2. Variables, literals, and constants

3. ASCII values

Program

#include<stdio.h>
int main()
{
char x;
printf("Enter a character:\n");
scanf("%c",&x); // %c format specifier for character values
printf("ASCII value of %c is %d",x,x); // %d is for integer value and %c is for character value
return 0;
}

 

Output

Explanation

char x; variable ‘x’ is declared of ‘char’ data type. And then, the user is asked to enter the value that will get stored in ‘x’ variable.

scanf(“%c”,&x); here we used %c format specifier to deal with that particular character that the user will enter.

printf(“ASCII value of %c is %d”,x,x); this statement prints the character value at the place of %c and the ASCII value of variable ‘x’ in place of %d.

The character is a ‘char’ type variable but the ASCII values are of integers types, thus we use %d for that.

 

Author

0 Comments

Submit a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Author