char Data Type in C Programming Language

Rumman Ansari   Software Engineer   2022-07-21   51688 Share
☰ Table of Contents

Table of Content:


  • char keyword is used to refer character data type.
  • Character data type allows a variable to store only one character.
char ch='a';
  • The storage size of character data type is 1(32-bit system). We can store only one character using character data type.
  • For example, 'A' can be stored using char datatype. You can't store more than one character using char data type.
Data Type Size(Byte) Range
char 1 -128 to 127 or 0 to 255

char Variable Declaration and Variable Initialization:

Variable Declaration: To declare a variable, you must specify the data type & give the variable a unique name.

 char flag ;  

Variable Initialization : To initialize a variable you must assign it a valid value.

 flag = 'a' ;  

char Variable Declaration and Initialization in two step:

char flag ;
flag = 'a' ;
Character Datatype in C Programming

Program

 
#include"stdio.h"
  
void main() {

   char flag; 
   flag = 'a' ;
   printf("%c \n",flag); 
 }

Outut

a
Press any key to continue . . .

char Variable Declaration and Initialization in single step:

 char flag = 'a' ;  
Character Datatype in C Programming

Program

 
#include "stdio.h"
  
void main() {

    char flag = 'a' ; 
    printf("%c \n",flag); 
 }

Outut

a
Press any key to continue . . .

A program that demonstrates char variables:

Program

 
 
#include "stdio.h"
  
void main() {

    char character1, character2;
 	character1 = 82; // code for R
    character2 = 'S';
    printf("character1 and character2: \n"); 
    printf("%c \n",character1);
	printf("%c \n",character2); 
 } 

Outut

character1 and character2:
R
S
Press any key to continue . . .

Explanation

Notice that character1 is assigned the value 82, which is the ASCII value that corresponds to the letter R. As mentioned, the ASCII character set occupies the first 127 values in the character set.