C Program to find ASCII Value of a Character

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 C 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;
}
C Program to find ASCII Value of a Single Character

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 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 ASCII Value of a Character 2

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 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

C Program to find the Sum of ASCII values of Characters in a String

Within this C program to find the 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, a 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 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).

Comments are closed.