Introduction
ASCII stands for American Standard Code for Information Interchange. It is a character encoding standard for electronic communication. Each character represented in ASCII has a unique numerical value, also known as ASCII code. In this lab, we will learn how to find the ASCII value of a character in C programming.
Create a new file
Create a new file main.c in the ~/project/ directory and paste the following code:
#include <stdio.h>
int main()
{
printf("\n\n\t\tLabEx - Best place to learn\n\n\n");
char c;
printf("Enter a character: ");
scanf("%c", &c);
printf("\n\nASCII value of %c = %d", c, c);
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
Understanding the code
Let's understand the code line by line.
printf("\n\n\t\tLabEx - Best place to learn\n\n\n");
This line is used to print a header on the console.
char c;
This line declares a variable c of type character.
printf("Enter a character: ");
This line prints a message asking the user to input a character.
scanf("%c", &c);
This line reads the input character from the user and stores it in the variable c.
printf("\n\nASCII value of %c = %d", c, c);
This line prints the ASCII value of the input character that was read in the previous line. The %c format specifier is used to print the character and %d format specifier is used to print the corresponding ASCII value.
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
This line is used to print a footer on the console.
return 0;
This line indicates that the program has finished executing and returns 0 as the exit status.
Compile and run the program
Compile and run the program using the following commands:
gcc main.c -o main
./main
Output:
LabEx - Best place to learn
Enter a character: A
ASCII value of A = 65
Coding is Fun !
Test the program
Enter different characters as input and observe the corresponding ASCII values that are printed on the console.
Summary
In this lab, we learned how to find the ASCII value of a character in C programming. The ASCII value of each character is a unique numerical value and is represented in C programming using the %d format specifier. The knowledge of ASCII values is important in various applications such as encryption algorithms and data encoding.



