C program to find the ASCII value of a character and vice versa
1 minsIn this article, you’ll learn how to find the ASCII value of a character in C. In addition to that, you’ll also learn how to do the opposite, that is, how to find the character corresponding to an ASCII value ranging from 0 to 127.
C program to find the ASCII value of a character
#include <stdio.h>
int main()
{
char c;
printf("Enter a character to find its ASCII value: ");
scanf("%c", &c);
int asciiValue = c; // char is automatically casted to an integer
printf("ASCII value of %c = %d\n", c, asciiValue);
return 0;
}
$ gcc CharToAsciiValue.c
$ ./a.out
Enter a character to find its ASCII value: H
ASCII value of H = 72
C program to find the character corresponding to an ASCII value
#include <stdio.h>
int main()
{
int n;
printf("Enter a number in the range 0 to 127: ");
scanf("%d", &n);
char c = n; // integer is automatically casted to a character
printf("Character corresponding to ASCII value %d = %c\n", n, c);
return 0;
}
$ gcc AsciiValueToChar.c
$ ./a.out
Enter a number in the range 0 to 127: 67
Character corresponding to ASCII value 67 = C