Declare a character in C
In this article, we will discuss how to declare a character in C with its syntax and example.
Declaring a character in C is as simple as declaring the other datatype variables but using the keyword "char". The char type is used to store a single character and is commonly used for processing text and ASCII characters.
Declaring a character:
You can use the "char" keyword to declare a character in C, followed by the variable name, and optionally initialize it with a character literal.
The syntax for declaring a character in C
If you want to declare a character variable to store the letter A, you can follow this syntax:
Char variable_name;
Char variable_name2 = 'A';
Program for declaring the character in C
Code:
#include<stdio.h>
int main(){
char capitalLetter = 'A';
char smallLetter = 'a';
char symbol = '@';
printf("capital letter is %c \n",capitalLetter);
printf("small letter is %c \n",smallLetter);
printf("symbol is %c \n",symbol);
}
Output:
![Declare a character in C](https://static.tutorialandexample.com/c/declare-a-character-in-c1.png)
Explanation:
Three Character variables are declared in this example: capitalLetter, smallLetter, and symbol. Each variable is assigned a specific character using single quotes.
capitalLetter is assigned the character 'A'.
smallLetter is assigned the character 'a'.
symbol is assigned the character '@'.
The printf() function is used to print the values of these character variables using the %c format specifier. The %c is used to indicate that the argument provided is a character.
Three separate printf() statements are used to print each character variable's value with appropriate messages:
The first printf prints: "capital letter is A".
The second printf prints: "small letter is a".
The third printf prints: "symbol is @".
Character literals:
A character literal represents a single character and is enclosed within single quotes. Character literals can represent actual characters, escape sequences, or special characters. Escape sequences are special sequences of characters that represent control characters or non-printable characters. Some commonly used escape sequences are \n (newline), \t (tab), \\ (backslash), \' (single quote), and \" (double quote).
Program for character literals
Code:
#include<stdio.h>
int main(){
char newLine = '\n'; // Escape sequence representing a newline
char tab = '\t'; // Escape sequence representing a tab
char backslash = '\\'; // Escape sequence representing a backslash
printf("first line %c second line %c",newLine,newLine);
printf("hello %c world \n",tab);
printf("%c printing the backslash in backslashes %c",backslash,backslash);
}
Output:
![Declare a character in C](https://static.tutorialandexample.com/c/declare-a-character-in-c2.png)
Explanation:
This program starts with the #include <stdio.h> directive, which includes the standard input-output library to enable input and output operations.
In the main() function, three character variables are declared: newLine, tab, and backslash. Each of these character variables is assigned a value using escape sequences:
newLine = '\n': It assigns the newline escape sequence to the newLine variable, which represents a newline character, causing the output to move to the next line.
tab = '\t': It assigns the tab escape sequence to the tab variable, which represents a horizontal tab character, adding whitespace to the output.
backslash = '\\': It assigns the backslash escape sequence to the backslash variable, which represents a backslash character. An escape sequence of two backslashes is used to represent a backslash itself.
The program uses three printf() functions to print various strings with the help of escape sequences.
The first printf() statement contains %c format specifiers to print the newLine character twice, which will result in two newlines in the output, creating two separate lines.
The second printf() statement contains %c format specifier to print the tab character, resulting in a horizontal tab before the string "world" in the output.
The third printf() statement contains %c format specifiers to print the backslash character twice, which will output two backslashes.
Taking input from the user and printing the characters in the console:
C provides formatted input/output functions to work with characters. The standard input/output functions like printf and scanf can be used to handle character data. You can use the %c format specifier with the printf() function to print a character. Next, you can use the %c format specifier with the scanf() function to read a character from the user.
Note the use of the ampersand & before inputChar in the scanf function. It is because the scanf() function needs the address of the variable to store the input
A program that will take input and print the character
Code:
#include<stdio.h>
int main(){
char character;
printf("Enter the character you wish to display \n");
scanf("%c",& character);
printf("The entered character is %c ", character);
return 0;
}
Output:
![Declare a character in C](https://static.tutorialandexample.com/c/declare-a-character-in-c3.png)
Explanation:
This code is only used to take the character from the user by using the scanf() function and printing the taken character by using the printf() function.