For working professionals
For fresh graduates
More
5. Array in C
13. Boolean in C
18. Operators in C
33. Comments in C
38. Constants in C
41. Data Types in C
49. Double In C
58. For Loop in C
60. Functions in C
70. Identifiers in C
81. Linked list in C
83. Macros in C
86. Nested Loop in C
97. Pseudo-Code In C
100. Recursion in C
103. Square Root in C
104. Stack in C
106. Static function in C
107. Stdio.h in C
108. Storage Classes in C
109. strcat() in C
110. Strcmp in C
111. Strcpy in C
114. String Length in C
115. String Pointer in C
116. strlen() in C
117. Structures in C
119. Switch Case in C
120. C Ternary Operator
121. Tokens in C
125. Type Casting in C
126. Types of Error in C
127. Unary Operator in C
128. Use of C Language
“How to find the ASCII value of a character in C?” or “How to print ASCII value in C?” Are these some of the many questions bothering you? Well then, worry no more.
Here is a detailed guide to everything you need to know about ASCII values. In this guide, we will learn to write a C program to find the ASCII value of a character using different methods. And that’s not all — we will also explore its multiple usages in the C programming language.
So, without any further ado, let’s get started!
Want to make your career is software engineering. Explore the upGrad's top Software Engineering Courses that are taught by the industry experts and top-faculty.
Since its inception in 1960, American Standard Code For Information Interchange or ASCII has received global recognition as a character encoding standard used in communication systems and computers.
A unique numeric code is assigned for each character, which can be anywhere between the range of 0-127. This includes various characters such as uppercase and lowercase, control characters, punctuation marks, and special symbols as well.
For example, while the ASCII code for the uppercase letter A is 65, the ASCII code for the lowercase letter ‘a’ is 97.
They are usually represented in the binary format, wherein each code is a 7-bit binary number. However, in today’s modern computing systems, they are represented using 8 bits (1 byte), the most significant bit being zero.
Let’s navigate other components to analyze this encoding standard in depth.
Simply put, an ASCII table can be defined as a reference chart that contains ASCII codes and their corresponding characters. It can be extremely beneficial when you are trying to look up specific characters and their codes.
An ASCII table consists of multiple columns, each highlighting some aspects of the characters. Let's look at some of the most common columns you will likely encounter in an ASCII table.
The ASCII table is easily accessible through any programming documentation, textbook, or even online. It can prove to be extremely useful, especially when you are trying to work with ASCII characters, encoding, or simply trying to understand the relationship between numeric codes and characters.
Converting a character into its corresponding ASCII value is a common programming phenomenon. In most programming languages, you can achieve this by simply using the built-in functions or methods provided by the language.
A few programming languages use type casting to convert characters into ASCII values. Such include C++ and Java, among others.
However, please note that type casting is only possible in C++ because the characters are internally represented as integers in ASCII or Unicode. Not all programming languages can support direct type casting from characters to integers. In such cases, you might need to use language-specific functions to obtain the ASCII value of a character.
To understand this example, you should have knowledge of the following C programming topics:
There are multiple ways to find the ASCII value of a character in C. Let’s go through the 3 most commonly used methods with examples and output.
This is the simplest way where we directly print the ASCII value of a character using %d.
#include <stdio.h>
int main() {
char ch = 'A'; // character to check
printf("The ASCII value of %c is %d\n", ch, ch);
return 0;
}
Output:
The ASCII value of A is 65
Explanation:
In this approach, we allow the user to enter any character at runtime and then print its ASCII value.
#include <stdio.h>
int main() {
char ch;
printf("Enter a character: ");
scanf("%c", &ch); // take character input
printf("The ASCII value of %c is %d\n", ch, ch);
return 0;
}
Sample Output:
Enter a character: g
The ASCII value of g is 103
Explanation:
You can also explicitly cast a character to int to get its ASCII value, though C does this implicitly too.
#include <stdio.h>
int main() {
char ch = 'z';
int ascii = (int) ch; // typecasting char to int
printf("The ASCII value of %c is %d\n", ch, ascii);
return 0;
}
Output:
The ASCII value of z is 122.
Explanation:
Now that you are familiar with the process of using a C program to find ASCII value of a character let’s take a look at some of the applications of ASCII values in C programming.
You can perform various character operations in C programming using ASCII values. Such include,
char c = 'a'; |
char c = 'A'; |
The output generated is D.
Simply put, a string is an array of characters. You can use ASCII values to perform a wide range of string operations in C programming. The following list contains some of the most commonly performed ones.
#include <stdio.h> |
However, please note that apart from this, you can also compare strings by manually comparing two individual characters.
From reading to writing, you can perform multiple file operations using ASCII values in C programming. A few such include,
Reading and Writing Characters To A File - To read or write a character to a file, we typically use the getc() and putc() I/O functions. The syntax for the same goes as follows,
getc()
char ch = getc(fptr);
putc()
putc(ch, fptr)
You need to first create a file pointer and select any filename of your choice. Following this, you can then use the putc() function to write characters to the file. After this has been done successfully, you can then move on to the getc() function to read the file data and display the same on the console.
To sum up, ASCII values play a significant role in C programming. From character representation to string manipulation, and standardisation, the usage of ASCII values in C ranges diversely. It enables programmers to perform various tasks and comparisons based on their values. Furthermore, a complete understanding of ASCII values and their usage in C programming also helps you to work with strings and file operations more effectively.
While grasping the basics of ASCII, check out upGrad’s Full Stack Software Development Bootcamp, which will further help you advance your technical skills and pave the way to explore a thriving career in development!
To get the ASCII value of a character in C, you can use a simple printf
statement with %d
format specifier. For example, printf("%d", ch);
will print the ASCII value of the character stored in variable ch
.
To convert a character to ASCII in C, store the character in a variable and print it using %d
. This treats the character as an integer and displays its corresponding ASCII value in the output.
Use scanf
to take a character as input from the user and then print it using %d
to get the ASCII value in C. This method helps you dynamically check the ASCII code of any typed character.
You can store the ASCII value of a char in an int variable by assigning the character directly to the int. For example:
int ascii = 'A';
This stores the ASCII value of 'A'
(65) in the ascii
variable.
In C, the ASCII value of lowercase 'a' to 'z' ranges from 97 to 122. These values are sequential, so you can loop through characters to display or work with each alphabet and its corresponding code.
Assign the character to an int
variable or print it using %d
. Example:
char ch = 'K';
printf("ASCII: %d", ch);
This prints the ASCII value of 'K'
, which is 75.
You don't need any special function to get ASCII value in C. Just use the %d
specifier in printf
to output the integer equivalent of a character directly from a char
variable.
Yes, you can use a loop to convert multiple characters to ASCII values in C. Iterate through a character array and print each character’s ASCII using %d
inside the loop.
Yes, since characters are stored as integers in C, you can perform arithmetic operations on them. For example, 'A' + 1
gives 'B'
, as ASCII values increase sequentially for alphabets.
Use scanf("%c", &ch);
to take a character input from the user, then print ch
using printf("%d", ch);
to get the ASCII value of a character in C.
Converting characters to ASCII in C is helpful for encryption, sorting, or performing logical checks. It allows you to treat characters as numeric data and apply custom algorithms or conditions based on their codes.
Take a Free C Programming Quiz
Answer quick questions and assess your C programming knowledge
Author|900 articles published
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918068792934
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.