C Program to Find the ASCII Value of a Character

In this article, we will write a C program to find the ASCII value of a character. The program takes a character input from the user and prints its ASCII value on the output window.

Sample Example:

Enter a character: A
ASCII value of A is 65

Enter a character: B
ASCII value of B is 66

Enter a character: a
ASCII value of a is 97

Before diving into the implementation of the program, let’s first try to understand what is an ASCII value.

An ASCII (American Standard Code for Information Interchange) value, also known as an ASCII code or character code, is a numerical representation of a character in the ASCII character set.

In ASCII, each character is represented by a 7-bit binary number (from 0 to 127). For example, the ASCII value of ‘A’ is 65, the ASCII value of ‘B’ is 66 and so on.

Basically, when you type a character, for example character ‘A’, your computer stores its ASCII value in the memory i.e. it stores 65 rather than storing the character ‘A’ itself. Similarly, when you type ‘B’, it stores 66 and so on.

In C programming, if you want to get the ASCII value of a character, you can simply store the character in a variable of type integer.

See implementation in the following program:

// C program to print ASCII
// value of a character

#include <stdio.h>

int main() {

	char ch;
	int ascii_value;
	
	printf("Enter a character: ");
	scanf("%c", &ch);
	
	// Get the ASCII value of character
	ascii_value = ch;
	
	printf("ASCII value of %c is %d", ch, ascii_value);
	    
    return 0;

}

Output:

Enter a character: A
ASCII value of A is 65

Enter a character: B
ASCII value of B is 66

Enter a character: a
ASCII value of a is 97

In the above program, when we stored the value of the character in a variable of type integer, we got its ASCII value instead of getting the character itself.

That’s all for this article. Thanks for reading!

Author

  • Manoj Kumar

    Hi, My name is Manoj Kumar. I am a full-stack developer with a passion for creating robust and efficient web applications. I have hands-on experience with a diverse set of technologies, including but not limited to HTML, CSS, JavaScript, TypeScript, Angular, Node.js, Express, React, and MongoDB.

    View all posts