Why are ASCII Values Needed in C?
ASCII values are essential in C because they enable the representation and manipulation of characters as integers. This helps in operations like string comparison, encoding, and storing characters in memory. Additionally, they allow for interaction between hardware and software, making it easier to process and transfer data. ASCII also ensures compatibility between different systems and devices that may use text-based communication.
How to Find ASCII Value of a Character in C?
In C, the ASCII value of a character can be found by simply assigning the character to an integer variable or by using the int type directly. When a character is assigned to an integer, its corresponding ASCII value is returned. Here's an example:
#include <stdio.h>
int main() {
char ch = 'A';
printf("The ASCII value of '%c' is %d\n", ch, ch); // %d gives the ASCII value
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output:
The ASCII value of 'A' is 65
In this example, the character 'A' is stored in the variable ch, and when printed as an integer (%d), its ASCII value (65) is displayed.
Examples to Implement ASCII Value in C
Example 1
Here is a program to print the ASCII value of the character variable.
#include <stdio.h>
int main()
{
char c;
printf("Enter any character ");
scanf("%c",&c);
printf("\nThe ASCII value of the entered character is: %d", c);
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output
Enter any character d
The ASCII value of the entered character is: 100
In this program, the first user will give the character input, and the character input is stored in variable c.
If we print the value of the 'c' variable by using %c format specifier, then it will print 'd' because we have given the character input as 'd'. But, here, we used the %d format specifier, so the ASCII value of 'A' will be printed, i.e., 100.
Also Read About, Sum of Digits in C
Example 2
Here is a program to print the ASCII value of Capital characters A to Z.
#include<stdio.h>
int main()
{
int i;
for(i=65;i<=90;i++) // for loop from 65 to 90
{
printf("The ASCII value of %c character is %d\n", i,i);
}
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output
The ASCII value of A character is 65
The ASCII value of B character is 66
The ASCII value of C character is 67
The ASCII value of D character is 68
The ASCII value of E character is 69
The ASCII value of F character is 70
The ASCII value of G character is 71
The ASCII value of H character is 72
The ASCII value of I character is 73
The ASCII value of J character is 74
The ASCII value of K character is 75
The ASCII value of L character is 76
The ASCII value of M character is 77
The ASCII value of N character is 78
The ASCII value of O character is 79
The ASCII value of P character is 80
The ASCII value of Q character is 81
The ASCII value of R character is 82
The ASCII value of S character is 83
The ASCII value of T character is 84
The ASCII value of U character is 85
The ASCII value of V character is 86
The ASCII value of W character is 87
The ASCII value of X character is 88
The ASCII value of Y character is 89
The ASCII value of Z character is 90
In the above C program, we will print the ASCII value of Capital characters A to Z. The ASCII value of Capital characters(A to Z) starts from 65 and ends at 90, so we iterate the for loop from 65 to 90.
Also read - Bit stuffing program in c
Example 3
Here is a program to print the ASCII value of the characters of a string.
#include<stdio.h>
int main()
{
char str[]="Ninjas";
int i=0;
while(str[i]!='\0')
{
printf("The ASCII value of character %c is %d\n", str[i],str[i]);
i++;
}
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output
The ASCII value of character N is 78
The ASCII value of character i is 105
The ASCII value of character n is 110
The ASCII value of character j is 106
The ASCII value of character a is 97
The ASCII value of character s is 115
In the above C program, we want to print the ASCII value of the characters of a string "Ninjas". So we executed the while loop, which prints the ASCII value of all the characters of a string "Ninjas" and you can try to run code on online compiler.
Must Read what is storage class in c and Decision Making in C
Applications of ASCII Values in C
- Character Manipulation: ASCII values allow C programs to manipulate characters easily by converting between characters and their corresponding ASCII codes.
- String Operations: C uses ASCII values extensively for string handling functions such as comparing strings, sorting characters, etc.
- Input/Output Operations: ASCII values facilitate reading and writing text data to files or other devices using standard I/O functions.
- ASCII Art: Creating ASCII art or graphical representations using characters is a creative use of ASCII values in C.
- Encoding and Decoding: ASCII values are fundamental in encoding and decoding text data for various purposes such as encryption and data transmission.
- Conditional Operations: ASCII values are used in conditional checks and switches to determine program flow based on character input.
Advantages of ASCII Values in C
- Simplicity: ASCII values simplify character handling and manipulation in C programming.
- Compatibility: ASCII is widely supported across different platforms and systems.
- Standardization: ASCII provides a standardized way to represent characters, ensuring consistency in data interpretation.
- Efficiency: Operations involving ASCII values are generally fast and straightforward due to their numeric nature.
Disadvantages of ASCII Values in C
- Limited Character Set: ASCII supports only basic English characters, limiting its usability for internationalization and non-English languages.
- Lack of Extended Features: Modern applications may require extended character sets and features not adequately covered by ASCII.
- Data Integrity Issues: ASCII does not provide built-in mechanisms for error detection or correction in text data, which can be crucial in certain applications.
Frequently Asked Questions
What is the full form of ASCII?
The full form of ASCII is American Standard Code for information interchange.
How many types of ASCII are there?
There are two types of ASCII codes.
Standard ASCII code uses a seven-bit encoding system.
Extended ASCII code uses an eight-bit system.
How many characters are in extended ASCII?
Extended ASCII supports the representation of 256 different characters.
What is the difference between ASCII and Unicode?
Ans: Unicode is used to process, store and facilitate the interchange of text data in any language. At the same time, ASCII represents text such as uppercase and lowercase letters, symbols, and digits(0-9), etc., in computers.
What is the ASCII value range of A to Z?
The ASCII value range for uppercase letters 'A' to 'Z' is from 65 to 90. Each letter is assigned a unique integer, where 'A' has an ASCII value of 65, and 'Z' has a value of 90.
Conclusion
Understanding ASCII values in C is crucial for efficient character manipulation, data processing, and system compatibility. ASCII provides a standardized way to represent characters as numeric values, making it easier to handle text-based data in programming. By leveraging ASCII values, C programmers can perform tasks like string comparison, character encoding, and memory management with greater precision and ease.