C Programming Tutorial

C Programming Tutorial

ASCII Value in C Programming

Hello Everyone, In this article, we will cover the concept of ASCII Value in C Programming. We will discuss how to work with ASCII values in C, including how to convert characters to their ASCII codes and vice versa. Whether you’re new to C or have some experience, this guide will help you to understand ASCII Value in C effectively. Let’s get started.

We all are familiers with ASCII Values. ASCII stands for American Standard Code for Information Interchange. It is a standard way of representing characters as binary numbers that can be easily processed by computers.

In C programming language, ASCII values are represented using integer values that correspond to the specific characters. Each character in the ASCII table is assigned a unique integer value ranging from 0 to 127. For example, the ASCII value for the letter ‘A’ is 65, ‘B’ is 66, and so on.

To get the ASCII value of a character in C, you can use the built-in function int which converts a character to its corresponding ASCII value. For example, if you want to get the ASCII value of the letter ‘A’, you can write:

char c = 'A';
int ascii_value = (int)c;

The ascii_value variable will now contain the integer value 65, which is the ASCII value of the letter ‘A’.

You can also convert an ASCII value back to its corresponding character by casting the integer value to a character type. For example:

int ascii_value = 65;
char c = (char)ascii_value;

The c variable will now contain the character ‘A’, which is the character corresponding to the ASCII value of 65.

C Program for how to get the ASCII value of a character:

#include <stdio.h>
int main() {
    char c;
    printf("Enter a character: ");
    scanf("%c", &c);
    printf("The ASCII value of %c is %d\\n", c, (int)c);
    return 0;
}

In this program, we first declare a variable c of type char to hold the character input by the user. We then use the printf function to prompt the user to enter a character and the scanf function to read in the character input.

Next, we use the printf function again to print out the ASCII value of the character entered by the user. The (int)c expression is used to convert the character c to its corresponding ASCII value, which is then printed to the console using the %d format specifier.

The output of the program will depend on the character input by the user. Here’s an example of what the output might look like if the user enters the letter ‘A’:

Enter a character: A
The ASCII value of A is 65

In this case, the program prompts the user to enter a character, and the user enters ‘A’. The program then calculates the ASCII value of ‘A’, which is 65, and prints it to the console along with a message indicating that it is the ASCII value of the character entered by the user.

Share This Article