C Format Specifiers & Escape Sequences
Format Specifiers & Escape Sequences
Whenever we write a program in C, we have to use format specifiers to define the variable type in input and output and escape characters to format the output.
Format Specifiers
A format specifier in C programming is used to define the type of data we are printing to the output or accepting through the input. Through this, we tell the compiler what type of variable we are using for input while using scanf() or output while using printf(). Some examples of format specifiers are %d, %c, %f, etc.
Here is a list of almost all format specifiers.
Format Specifier | Type |
%c | Used to print a character |
%d | Used to print the signed integer |
%f | Used to print the float values |
%i | Used to print the unsigned integer |
%l | Used to print the long integer |
%lf | Used to print the double values |
%lu | Used to print the unsigned integer or unsigned long integer |
%s | Used to print the string |
%u | Used to print the unsigned integer |
One example is shown below.
Output
The %s used in the printf( ) is a format specifier. This format specifier tells printf( ) to consider it as a string and print accordingly.
Escape Sequences
Many programming languages support the concept of Escape Sequences. An escape sequence is a sequence of characters that are used in formatting the output. They are not displayed on the screen while printing. Each character has its specific function. For example, \t is used to insert a tab, and \n is used to add a new line.
Here’s the list of all escape sequences
Escape Sequence | Description |
\t | Inserts a tab space |
\b | Inserts a backspace |
\n | Inserts a new line |
\r | Inserts a carriage return |
\f | Inserts a form feed |
\’ | Inserts a single quote character |
\’’ | Inserts a double quote character |
\\ | Inserts a backslash character |
One example is shown below,
Output