C Character Constant
Single Character Constant :
- Character Constant Can hold Single character at a time.
- Contains Single Character Closed within a pair of Single Quote Marks
- Single Character is smallest Character Data Type in C.
- Integer Representation : Character Constant have Integer Value known as ‘ASCII’ value
- It is Possible to Perform Arithmetic Operations on Character Constants
Examples of character Type :
- ‘a’
- ‘1’
- ‘#’
- ‘<‘
- ‘X’
How to Declare Character Variable ?
Way 1: Declaring Single Vaiable
char variable_name;
Way 2: Declaring Multiple Vaiables
char var1,var2,var3;
Way 3: Declaring & Initializing
char var1 = 'A',var2,var3;
Format Specifier for Character Variable :
- “%c” is used as format specifier for character inside C.
- However we can also use “%d” as format specifier because “Each Character have its equivalent interger value known as ASCII Value. “
Using Format Specifier to Print Character Variable :
Sample 1:
printf("%d",'a'); //Output : 97
Sample 2:
printf("%c",'97'); //Output : a
These two Represent the same Result
Complete Example of Character Variable :
Example 1 : Creating Variable and Displaying Character Data
#include<stdio.h> int main() { char cvar = 'A'; printf("Character is : %c",cvar); return(0); }
Output :
Character : A
Example 2 : Accepting Character Data
#include<stdio.h> int main() { char cvar; printf("Enter chracter :"); scanf("%c",cvar); printf("Character is : %c",cvar); return(0); }