C++ Character

Character Constant


  • Character constant is a single characters enclosed in single quotes.
  • It contains alphabet, digit, special character and blank space.
  • e.g. 'b' , '$' , '@' , '5' , etc.

Character Variable


character variable is just like a normal variable. We can also declare it like other variables.
E.g. char name;

Syntax


char var='character';


Here, char used to declare character variable.
var refers to character variable defined by the programmer.
character specifies single character to be stored in character variable.

Program to initialize and display a character.



#include<iostream.h>
#include<conio.h>
void main()
{
      clrscr();
      char ans='Y';
      cout<<"\nAnswer = "<<ans;
      getch();
}



Output


Answer  = Y


Program to read and display Character


#include<iostream.h>
#include<conio.h>
void main()
{
       clrscr();
       int ans;
       cout<<"\nEnter Your Answer Y/N=";
       cin>>ans;
       cout<<"\nAnswer = "<<ans;
       getch();
}


Output


Enter Your Answer Y/N= Y
Answer=Y

Back To Top