ASCII value in C

ASCII value in C

Hey everyone, Do you know what is ASCII or ASCII value? Today we will discuss about these ASCII value in C.

What is ASCII code?

ASCII stands for American Standard Code for Information Interchange. It is a character encoding scheme that is used for electronics communication. Each character or a particular character is represented by some ASCII code, and each ASCII code holds 7 bits in memory.

In C programming language, a character variable does not have a character value instead of the ASCII value of the character variable. The SCI value represents the number of character variables, and each character variable is assigned from 0 to 127 with some number range. For example, the ASCII value of ‘A’ is 65.

In the example above, we assign ‘A’ to a character variable that has an SCI value of 65, so 65 will be stored in a character variable instead of ‘A’.

ASCII value in C
ASCII value in C

Let us understand through an example.

We will create a program that will display the SCI value of the character variable.

#include <stdio.h>
 int main()  {    
  char ch;    // variable declaration    
  printf("Enter a character");  
    scanf("%c",&ch);  // user input    
  printf("\n The ASCII value for this ch variable is : %d", ch);  
    return 0;  
} 

In the above code, the first user will input the character, and the input will be stored in the ‘ch’ variable. If we print the value of ‘ch’ variable using% c format spacier, it will display ‘A’ as we have given the character input as ‘A’, and if we use% d format spacier If done, its ascii value will be displayed, that is, 65.

ASCII value in C

The output above shows that the user gave the input as ‘A’, and after giving the input, the SCI value of ‘A’ will be printed, that is, 65.

Now, we will create a program that will display the SCI value of all characters.

#include <stdio.h>  int main()  { 
  int k;   // variable declaration    
for(int k=0;k<=255;k++)  // for loop from 0-255   {   
    printf("\n The ASCII value of %c is %d", k,k); 
  } 
 return 0; 
 }  

The above program will display the ASCII value of all characters. As we know that the SCI value of all characters starts at 0 and ends at 255, so we set the distance from 0 to 255 for the loop.

Now we will create a program that will sum the string’s SCI value.

#include <stdio.h>  
int main()  {  
    int sum=0;  // variable initialization     
 char name[20];  // variable initialization
      int i=0;  // variable initialization 
     printf("Enter a name: ");   
   scanf("%s", name);  
    while(name[i]!='')  // while loop      {    
      printf("\n The ASCII value of the character %c is %d", name[i],name[i]);          
sum=sum+name[i];      
    i++;    
  } 
     printf("\n Sum of the ASCII value of a string is : %d", sum);   
   return 0;  
} 

In the above code, we are taking user input as a string. After taking user input, we execute a loop that combines the SCI value of all the characters in a string and stores it in the ‘sum’ variable.

How does ASCII Value Represent Internally in C?

1. Take an example string as “ABCDEFG HIJK LMNO”.

2. When we give this instruction to the machine it will not store it as “ABCDEFG HIJK LMNO”, but will store the equivalent ASCII value.

3. Hence now the machine stored value is “65 66 67 68 69 70 71 32 72 73 74 74 75 32 76 77 78 79”.

4. The value of A according to ASCII is 65, B is 66, C is 67, and so on. Space ASCII value is: 32

Syntax:
int x;
for(int x=0;x<255;x++){
Printf("%c,%d",x,x);   //%c is for display 
character and %d is for ASCII value
}

Examples to Implement of ASCII Value in C

Below are the examples.

1. Capital A to Z ASCII Values.

Code:

//including basic C libraries
#include <stdio.h>
//main method for run C application
int main()
{
//declaration int variable
int capitalChars;
//iterating Capital ASCII value 
for(capitalChars=65;capitalChars<91;capitalChars++)  // for loop from 65 to 90
{//display ASCII values for its equivalent characters
printf("\n \t The equivalent ASCII for  %c character is %d", capitalChars,capitalChars);
}
return 0;
}

2. Small A to Z ASCII Values

Code:

//including basic C libraries
#include <stdio.h>
//main method for run C application
int main(){
//declaration int variable
int lowerChars;
//iterating lowe case characters ASCII values
for(lowerChars=97;lowerChars<123;lowerChars++)  // for loop from 97 to 122
{
//display ASCII values for its equivalent characters
printf("\n The equivalent ASCII for lower case %c character is %d", lowerChars,lowerChars);
}
return 0;
}

3. Space ASCII Value

Code:

//including basic C libraries
#include <stdio.h>
int main(){
int space;
for(space=32;space<33;space++)  
{
printf("\n \t The equivalent ASCII for space %c is %d", space,space);
}
return 0;
}

4. Special Characters ASCII Values

#include <stdio.h>
int main(){
int specialChars;
for(specialChars=33;specialChars<48;specialChars++)  
{
printf("\n\tThe equivalent ASCII for %c special character is %d", specialChars,specialChars);
}
for(specialChars=58;specialChars<65;specialChars++)  
{
printf("\n\tThe equivalent ASCII for %c special character is %d", specialChars,specialChars);
}
for(specialChars=123;specialChars<127;specialChars++)  
{
printf("\n\tThe equivalent ASCII for %c special character is %d", specialChars,specialChars);
}
return 0;
}

5. All ASCII Values in One Place

Code:

#include <stdio.h>
int main(){
int allChars;
for(allChars=0;allChars<256;allChars++) 
{
printf("\n\tThe ASCII value of %c is %d", allChars,allChars);
}
return 0;
}

6. Given Name ASCII Values

Code:

#include <stdio.h>
int main(){
char charArray[20];
int var=0;
printf("\n\tPlease enter you name to know your name ASCII values: ");
scanf("%s", charArray);
while(charArray[var]!='\0')  // iterating array characters sequentially
{
printf("\n\tThe ASCII value of character %c is %d", charArray[var],charArray[var]);
var++;
}
return 0;
}

Conclusion

ASCII in C is used to denote numeric values ​​for each character. This character is stored internally as an ASCII value, but the same character that we have given. We can display lower case, upper case letters, special characters etc. ASCII values ​​using our respective command. We currently have 255 ASCII characters in C.

Leave a Comment