Detailed Explanation of ASCII in C Language

What is ASCII? ASCII stands for American Standard Code for Information Interchange. It is a character encoding scheme used for electronic communication. Each character or special character is represented by an ASCII code, and each ASCII code occupies 7 bits in memory.
In the C programming language, a character variable does not contain the character value itself, but rather the ASCII value of the character variable. The ASCII value represents the character variable as a number, and each character variable is assigned a value in the range of 0 to 127. For example, the ASCII value of ‘A’ is 65.
In the example above, we assign ‘A’ to the character variable, and its ASCII value is 65, so 65 will be stored in the character variable instead of ‘A’.
Let’s understand this with an example.
We will create a program to display the ASCII value of a 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 of the ch variable is : %d", ch);     return 0;  }
In the above code, the user will input a character, and the input will be stored in the variable ‘ch’. If we print the value of the ‘ch’ variable using the %c format specifier, it will display ‘A’, because the character input we provided is ‘A’. If we use the %d format specifier, it will display its ASCII value, which is 65.

πŸ‘‡ Click to ClaimπŸ‘‡

πŸ‘‰ Collection of C Language Knowledge Materials

Output Result

Detailed Explanation of ASCII in C Language

The output above shows that the user input was ‘A’, and after the input, it prints the ASCII value of ‘A’, which is 65.
Now, we will create a program to display the ASCII values of all characters.
#include <stdio.h>
int main(){   int k;   // variable declaration   for (k = 0; k <= 255; k++)  // loop from 0 to 255  {       printf("\nThe ASCII value of %c is %d", k, k);  }   return 0;}
The above program will display the ASCII values of all characters. Since we know that the ASCII values of all characters start from 0 and end at 255, we use a for loop to iterate from 0 to 255.
Now we will create a program to calculate the sum of the ASCII values of a string.
#include <stdio.h>
int main(){   int sum = 0;  // variable initialization   char name[20];  // variable initialization   int i = 0;  // variable initialization
   printf("Please enter a string:");   scanf("%s", name);
   while (name[i] != '\0')  // loop  {       printf("\nThe ASCII value of character %c is %d", name[i], name[i]);       sum = sum + name[i];       i++;  }
   printf("\nThe sum of the ASCII values of the string is: %d", sum);
   return 0;}
In the above code, we receive user input as a string. After receiving the user input, we perform a while loop to add the ASCII values of all characters in the string and store the result in the variable ‘sum’.
Output Result
Detailed Explanation of ASCII in C Language
Programmer Technical Exchange Group

Scan the code to join the group and remember to note: city, nickname, and technical direction.

Leave a Comment