Table of Contents
Introduction
There is a big difference between the storage of a character value and the assignment of a character value. Whenever any value is assigned to a variable, that particular value is stored in the memory. In the previous article, we explained how to assign a character value to a variable. In this article we will discuss Character Value Storage in C.
But the storage of a character value can be considered an exceptional case because whenever a character value is given to a variable of type char, its ASCII value gets stored, not the character value itself.
ASCII – American Standard Code For Information Interchange. According to this scheme, there are 256 characters (including special characters) in a programming language, and every character is represented by a numeric value known as ASCII value.
Example
For example, the ASCII value for ‘A’ is 65, and for ‘a’ is 97.
When we want to print a character, we use %c, which will display the character value. If in place of %c, %d is used, its integer value, i.e. the ASCII corresponding to that particular variable, will be displayed.
#include<stdio.h> int main() { char z; printf("enter a character value\n"); scanf("%c",&z); printf("character value is : %c\n",z); printf("ASCII value is : %d",z); return 0; }
Output
Code Explanation
(In the scanf() function, we will use %c because we want to input a character value.)
In the above code, when %c is used in printf( ) statement, it prints character ‘d’ as an output ( shown in the output window) but with %d in printf( ) statement. 100 is printed, which is the ASCII value of character ‘d’.
This portion is significant because we know what gets stored in the memory block allocated to a character variable and how the compiler knows about the character value, i.e., their ASCII values.
0 Comments