Format Specifiers & Escape Sequences
Format Specifiers & Escape Sequences in C
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:
#include <stdio.h>
int main() {
char c[100] = "BTX-D9";
printf("Printing a string, %s.", c);
}
Output:
Printing a string, BTX-D9.
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:
#include <stdio.h>
int main() {
printf("Printing inside a double quotation, \"BTX\"");
}
Output:
Printing inside a double quotation, “BTX-D9”.