C Character Constant



Single Character Constant :

  1. Character Constant Can hold Single character at a time.
  2. Contains Single Character Closed within a pair of Single Quote Marks
  3. Single Character is smallest Character Data Type in C.
  4. Integer Representation : Character Constant have Integer Value known as ‘ASCII’ value
  5. It is Possible to Perform Arithmetic Operations on Character Constants

Examples of character Type :

  1. ‘a’
  2. ‘1’
  3. ‘#’
  4. ‘<‘
  5. ‘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);
}