What is ASCII ?
The full form of ASCII is American Standard Code for Information Interchange. It is a character encoding scheme used for electronic communications. Each character or special character is represented by some ASCII code, and each ASCII code occupies 7 bits in memory.
In the C programming language, the character variable contains the ascii value of the character variable, not the character value itself. ASCII values ​​represent variable characters as numbers, and each variable character is assigned a number between 0 and 127. For example, the ascii value of ‘A’ is 65.
In the above example, we have assigned ‘A’ to a character variable with ascii value 65, so 65 and not ‘A’ will be stored in the character variable.
Syntax
To get the ASCII value of a character in C, you can use an int file to store the ASCII value and the character itself must be enclosed in an expression. For example:
char ch = ‘A’;Â Â Â Â // Character variable
int ascii_value;Â Â // Variable to store ASCII value
ascii_value = ch;Â // Assigning ASCII value to variable
- To print the ASCII value, you can use the printf() function with the %d format specifier:
printf(“ASCII value of %c is %d\n”, ch, ascii_value);
Example
Here is a complete example showing how to get and print the ASCII value of a character.
#include <stdio.h>
int main() {
char ch = ‘A’;Â Â Â Â // Character variable
int ascii_value;Â Â // Variable to store ASCII value
ascii_value = ch;Â // Assigning ASCII value to variable
printf(“ASCII value of %c is %d\n”, ch, ascii_value);
return 0;
}
Output
ASCII value of A is 65