C Program to find ASCII Value of a Character

How to write a C Program to find 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 into the variable, instead of storing the character itself, the ASCII value of that character will store.

C Program to find ASCII Value of a Character

In this C program to find the ASCII value of a character, the User asked to enter any character, and this program will display the ASCII value of that character.

For example, the ASCII value for the A is 65. Please refer ASCII Table article to understand the list of ASCII characters and their decimal, Hexadecimal, and Octal numbers.

/* C Program to find ASCII Value of a Character */
#include <stdio.h>

int main()
{
  char ch;
  
  printf("\n Please Enter any character \n");
  scanf("%c",&ch);
  
  printf("\n The ASCII value of given character = %d",ch);
  return 0;
}
 Please Enter any character 
a

 The ASCII value of given character = 97

Within this C Program to find ASCII Value of a Character 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 2nd printf statement, We are just displaying the input value, but we used the format string %d instead of using %c. That’s why it is showing 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 find ASCII value of all Characters

This C program will print the ASCII values of all the characters currently present.

/* C Program to find ASCII Value of a Character */
#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 (ASCII values Table) of the program to find the ASCII value of a character example, you can understand that the ASCII values of all the characters will be between 0 and 255. That’s why we used 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 ASCII Value of a Character 2

C Program to find Sum of ASCII value in a String

This program to find the ASCII value of a character allows the user to enter any valid string. Next, this program will display the ASCII value of each character present in that string and calculate the Sum of all those ASCII 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);

 while(name[i]!='\0')
  {
   printf("The ASCII value of the Character %c = %d\n",
   name [i], name [i]);
   sum = sum + name [i];
   i++;
  }
 printf("\nSum of all characters : %d ", sum);
}

C Sum of ASCII value in a String output

Please Enter any name You wish
SURESH

The ASCII value of the Character S = 83
The ASCII value of the Character U = 85
The ASCII value of the Character R = 82
The ASCII value of the Character E = 69
The ASCII value of the Character S = 83
The ASCII value of the Character H = 72

Sum of all characters : 474

Within this c program to find ASCII value of a character example,

  • 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, special character \0 is added at the end to mark where it ends. So in the 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 reached then, the loop will terminate.
  • In the next line, we are just displaying the individual character and its ASCII value.
  • Next line, we are adding those ASCII values of every character present in the string. Next, we increment the value of i.
  • Last printf statement will display the output of Sum (Nothing but the Sum of ASCII value in a String).

Comments are closed.