top

Search

C Tutorial

.

UpGrad

C Tutorial

C Program to Find ASCII Value of a Character

Overview

“How to get ASCII value of char 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 value and its multiple usages in the C programming language. 

So, without any further ado, let’s get started!

What is ASCII code?

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.

ASCII Table

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. 

  • Decimal - As the name suggests, the decimal column contains the numeric value that is assigned to each character. 

  • Hexadecimal - It highlights the ASCII codes in base 16 formats. 

  • Octal - The ASCII codes in this column are highlighted in base-8 format. 

  • Character - It typically consists of various characters in correspondence to the ASCII codes. They may range from special characters to symbols and letters. 

  • Description - It usually contains a brief description or name of each character. Sometimes, it also specifies the function or purpose of a character. 

ASCII Table Chart

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. 

Conversion of Character to ASCII Value

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. 

Using Type Casting to Convert Character to ASCII Value

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. 

Example of Converting Character to ASCII Value

Here is a detailed example of how you can use a C program to find ASCII value of a character.

#include <stdio.h>
int main() {
    char c = 'J';
    int asciiValue = (int)c;
    printf("%d\n", asciiValue);
    
    return 0;
}

As clearly visible in this above-mentioned example, we have assigned the character variable ‘c’ a value, ‘J’. Using type casting ‘(int)’, we have converted the character ‘c’ to an integer, thus generating its ASCII value. We have then used the integer variable ‘ asciiValue’ to store the ASCII value and displayed it on the console, with the help of ‘printf’. The output for this would be 106.

Application of ASCII Values in C Programming

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. 

Character Operations using ASCII Values

You can perform various character operations in C programming using ASCII values. Such include,

  • Converting Case: Converting a character from uppercase to lowercase is very easy. You simply have to manipulate the ASCII value. For example, if you wish to convert it into an uppercase, you can subtract 32 from its ASCII value. Simultaneously, if you want to convert it into a lowercase character, you can add 32. Let’s take a look at this example,

char c = 'a';
c = c - 32; // Convert to uppercase
printf("%c\n", c);
  • Comparing Character: To compare characters, you must first compare their ASCII values. For example, to check whether a character is an uppercase letter or not, you can take the help of a comparison operator, such as (‘<’ and ‘>’) with the appropriate ASCII value ranges. 

  • Arithmetic Operations: By simply adding or subtracting a number from a character, you can shift it to a completely different character. Here is a small example of the same.

char c = 'A';
c = c + 3; // Shift by 3 characters
printf("%c\n", c);

The output generated is D. 

String Operations using ASCII Values

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. 

  • String Comparison - By analysing the ASCII value of two characters, you can compare two strings. To achieve the same, you can make use of library functions such as ‘strcmp()’. The algorithm for the same goes as follows,

#include <stdio.h>
#include <string.h>
int main() {
    char str1[] = "Hello";
    char str2[] = "People";
    int result = strcmp(str1, str2);
    if (result < 0) {
        printf("str1 is less than str2\n");
    } else if (result > 0) {
        printf("str1 is greater than str2\n");
    } else {
        printf("str1 is equal to str2\n");
    }
    return 0;
}

However, please note that apart from this, you can also compare strings by manually comparing two individual characters. 

  • String Length - You can also accurately determine the length of a string using ASCII values. The idea is to count the characters until you reach the null character ('\0').

  • String Manipulation - Last but not least, string manipulation is also achievable through ASCII values. It enables the execution of a variety of tasks ranging from altering the case of characters to replacing characters, among other transformations. 

File Operations using ASCII Values

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. 

Conclusion

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!

FAQs

Q1: Can you state the ASCII value of A to Z?

For uppercase letters A-Z, the ASCII values fall within the range of 65-90. Whereas, for lowercase letters a-z, the ASCII values range from 97-122. 

Q2: Where can I find ASCII characters?

You can quite easily get access to the ASCII table to identify the ASCII value of any character. It contains a detailed description of each character to its assigned value ranging from 0-127.

Q3: What is the total number of ASCII characters in C?

There are as many as 256 ASCII characters in the C programming language. 

Leave a Reply

Your email address will not be published. Required fields are marked *