How to write a C Program to find the ASCII Value of a Character with an example?. In C programming, every character has its own ASCII value (Nothing but an integer). Whenever we store a character in the variable, instead of storing the character itself, the ASCII value of that character will be stored.
C Program to find the ASCII Value of a Character
In this program, the User is asked to enter any character, and this program will find and display the ASCII value of that character.
For example, the ASCII value for the A is 65. Please refer to the ASCII Table article to understand the list of ASCII characters and their decimal, Hexadecimal, and Octal numbers.
#include <stdio.h>
int main()
{
char ch;
printf("\n Please Enter any character \n");
scanf("%c",&ch);
printf("\n The ASCII value = %d",ch);
return 0;
}

Within this example, the first printf statement will ask the user to enter any character. Next, the character assigned to variable ch.
There is nothing unusual in the 2nd printf statement. We just displayed the input value, but we used the format string %d instead of %c. That’s why it shows an integer value (I mean ASCII value). If you want to display the character value, replace %d with %c in the above C Programming code.
C Program to Print ASCII value of a character using a function
We defined a function of type int, which means it returns an integer.
#include <stdio.h>
int ASCIICode(char ch)
{
return ch;
}
int main()
{
char ch;
printf("Enter any character = ");
scanf("%c", &ch);
printf("ASCII value of %c = %d", ch, ASCIICode(ch));
}
Enter any character = A
ASCII value of A = 65
Display ASCII values for a range of characters
In the above example, we used the “%d” format specifier to convert or display the ASCII value of a single character. However, if we use a for loop to iterate over a range of characters, we can display the ASCII values of each character. In this program, we will display ASCII values for a range of characters from a to z. Refer to for loop in C.
#include <stdio.h>
int main()
{
for (char ch = 'a'; ch <= 'z'; ch++)
{
printf("ASCII value of %c = %d\n", ch, ch);
}
}
ASCII value of a = 97
ASCII value of b = 98
ASCII value of c = 99
ASCII value of d = 100
ASCII value of e = 101
ASCII value of f = 102
ASCII value of g = 103
ASCII value of h = 104
ASCII value of i = 105
ASCII value of j = 106
ASCII value of k = 107
ASCII value of l = 108
ASCII value of m = 109
ASCII value of n = 110
ASCII value of o = 111
ASCII value of p = 112
ASCII value of q = 113
ASCII value of r = 114
ASCII value of s = 115
ASCII value of t = 116
ASCII value of u = 117
ASCII value of v = 118
ASCII value of w = 119
ASCII value of x = 120
ASCII value of y = 121
ASCII value of z = 122
C Program to find the ASCII value of all Characters
This program will print the ASCII values of all the characters currently present.
#include <stdio.h>
int main()
{
int i;
for(i=0;i<=255;i++)
{
printf("The ASCII value of %c = %d\n",i,i);
}
return 0;
}
From the below images (Table) of the program to find the ASCII value example, you can understand that the ASCII values of all the characters will be between 0 and 255. That’s why we used the for loop to start at 0 and iterate the loop up to 255. If the number is more than 255, then the condition will fail.

C Program to find the Sum of ASCII values of Character in a String
This program to find the ASCII value of a character allows the user to enter any valid string. Next, this C program will display the ASCII value of each character present in that C string and calculate the Sum of all those values.
#include <stdio.h>
#include <string.h>
void main()
{
int i = 0, sum = 0;
char name[50];
printf("\nPlease Enter any name You wish\n");
scanf("%s", name);
printf("\nThe ASCII value of the Characters\n");
while(name[i]!='\0')
{
printf("%c = %d\n",
name [i], name [i]);
sum = sum + name [i];
i++;
}
printf("\nSum of all characters : %d ", sum);
}
The sum of ASCII value in a String output

Within this C program,
- The first printf statement will ask the user to enter any name or string, and that string will assign to the character array name[50].
- For every string, a special character \0 is added at the end to mark where it ends. So in the C while loop, we check for the same: while(name[i]!=’\0′)
- The while loop will continue iterating until it reaches the special character \0. Once it reaches, then the loop will terminate.
- In the next line, we just display the individual character and its ASCII value.
- In the next line, we add the ASCII values of every character present in the string. Next, we increment the value of i.
- The last printf statement will display the output of Sum (Nothing but the Sum of ASCII value in a String).
Also Read
Comments are closed.