C Program to Print ASCII Value of a Character

A character in C programming language is stored as a particular integer in memory location. The integer value corresponding to a character is know as it's ASCII value. For Example, the ASCII value of 'A' is 65.

A character and it's ASCII value can be used interchangeably. That's why we can perform all arithmetic operations on characters line 'A' + 3, 'A'/4 etc. If any expression contains a character then it's corresponding ASCII value is used in expression. When we store a character in a variable of data type char, the ASCII value of character is stored instead of that character itself.

The C Program to Print the ASCII Value of a Character is a simple yet important exercise that introduces beginners to ASCII encoding and character manipulation in C programming. In this article, we will explore the advantages of this program, its importance for beginners, and a conclusion highlighting its educational value.


Importance of Practicing the C Program to Print ASCII Value of a Character for Beginners

  • Understanding ASCII Encoding : This program helps beginners understand the ASCII encoding scheme, which is used to represent characters in computers.

  • Character Manipulation : Implementing this program involves basic character manipulation, such as reading input characters and printing their ASCII values.

  • Fundamental Concept : The ASCII encoding is a fundamental concept in computer science, and practicing this program helps beginners grasp this concept early in their programming journey.

  • Conceptual Understanding : Writing this program enhances beginners' conceptual understanding of how characters are represented and manipulated in computer programs.

  • Preparation for Advanced Concepts : The skills and concepts learned from this program prepare beginners for more advanced programming concepts that involve character manipulation.

  • Enhanced Problem-Solving Skills : Implementing this program enhances beginners' problem-solving skills by challenging them to convert characters to their ASCII values.


C program to print the ASCII value of a character

In this program, we take a character as input from user and prints the ASCII value of input character %d format specifier.

#include <stdio.h>

int main() {
    char c;
    
    printf("Enter a Character\n");
    scanf("%c",&c);
    /*Prints the ASCII value as integer */
    printf("ASCII value of %c = %d",c,c);
    
    return 0;
}
Output
Enter a Character
A
ASCII value of A = 65

C program to print the ASCII value of all alphabets

The ASCII value of alphabets are consecutive natural numbers. If we increment the ASCII value of 'C', we will get the ASCII value of 'D'. In this program, we will print the ASCII value of lower and upper-case alphabets using for loop.

#include <stdio.h>

int main() {
    int i;
    
    for(i = 0; i < 26; i++){
       printf("%c = %d   |   %c = %d \n",
          'A'+i,'A'+i,'a'+i,'a'+i);
    }
    
    return 0;
}
Output
A = 65   |   a = 97 
B = 66   |   b = 98 
C = 67   |   c = 99 
D = 68   |   d = 100 
E = 69   |   e = 101 
F = 70   |   f = 102 
G = 71   |   g = 103 
H = 72   |   h = 104 
I = 73   |   i = 105 
J = 74   |   j = 106 
K = 75   |   k = 107 
L = 76   |   l = 108 
M = 77   |   m = 109 
N = 78   |   n = 110 
O = 79   |   o = 111 
P = 80   |   p = 112 
Q = 81   |   q = 113 
R = 82   |   r = 114 
S = 83   |   s = 115 
T = 84   |   t = 116 
U = 85   |   u = 117 
V = 86   |   v = 118 
W = 87   |   w = 119 
X = 88   |   x = 120 
Y = 89   |   y = 121 
Z = 90   |   z = 122

Tips for Writing Print ASCII Value Program in C

  • Range of ASCII Values : Remember that ASCII values range from 0 to 127 for standard ASCII characters.

  • Character vs. Integer : Understand the difference between a character and its ASCII value (an integer).

  • Input Validation : Validate user input to ensure that a valid character is entered.

  • Efficient Code : Write efficient code by minimizing unnecessary operations and using appropriate data types.

  • Use of Built-in Functions : Utilize built-in functions in C, such as printf, to print the ASCII value of a character.

  • Platform Dependence : Be aware that the actual ASCII values may vary depending on the platform and character encoding used.

Conclusion

In conclusion, the C Program to Print the ASCII Value of a Character is a valuable exercise that introduces beginners to ASCII encoding and character manipulation in C programming. Practicing this program is important for beginners to develop a solid understanding of these concepts, which are foundational for more advanced programming tasks.


Related Topics
C program for Input/Output of Integer, Character and Floating point numbers
C program to check whether an alphabet is a vowel or consonant
C program to add digits of a number
C program to check whether a character is alphabet or not
C program to check whether a character is alphabet or digit
C program to check whether a character is vowel or consonant using switch statement
C program to check a number is odd or even using conditional operator
C program to check odd or even numbers
C program to swap two numbers
List of all C programs