Introduction to characters in C
A character constant is a single characters enclosed in single quotes (‘). A character constant can contain an alphabet, digit, special character or a white space. A character constant can’t contain more than one character in it.
‘Z’ , ‘9’ , ‘#’ etc.
A character variable is a variable of char data type. The character variable can’t hold more than one character. A character variable can be defined as we define other types of variables in C language.
In the above example choice is a character variable which can’t hold more than one character in it.
Initialization of a character variable
We can initialize a character variable as we initialize variables of other data types. The syntax to initialize a character variable is
char is a data type as well as a keyword which specifies that a character variable is being declared.
var_name refers to the name of character variable defined by the programmer. The variable name must follow all the rules of a valid identifier of C Language.
character specifies the single character to be stored in character variable. It must be enclosed within single quotes(‘).
In the above example, char variable choice has been initialized with the value ‘N’.
We can also store any of the escape sequences in a character variable.
|Program to demonstrate the use of character variable|
Reading and Displaying a character variable
We can read and display a character variable just as we read and display a variable of other data type. We can use input functions like scanf(), getche(), getch(), getchar() etc. to read value of a character variable and can use output functions like printf(), putchar(), putch() to display value of a character variable
|Program to read and display a character variable.|
printf(“\nEnter any character”);
|Enter any characterj|
Best Books of C