Escape Sequences and Format Specifiers in C Programming Language
|Two basic topics that every programmer in C should be familiar with is Format specifier and Escape sequences. Escape sequences allow the user to access certain functionalities that are not available otherwise. Format specifiers are used for working on various data types.
Escape Sequences
Escape sequences are a sequence of characters that represent a different meaning when used in a string literal. Escape sequences are the same for many other programming languages like java, c++, c#, etc. They usually begin with the ‘\(backslash)’ symbol. Here is a list of escape sequences:
ESCAPE SEQUENCES | DESCRIPTION |
---|---|
\a | alarm or beep or bell |
\b | backspace |
\f | Form feed |
\n | New Line |
\r | Carraige Return (reset the device’s position to the beginning of a line) |
\t | Horizontal Tab |
\v | Vertical Tab |
\\ | Backslash |
\’ | Single Quote |
\” | Double Quote |
\? | Question Mark(used to avoid trigraphs) |
\nnn | Interpreted as an Octal Number |
\xhh | Interpreted as a Hexadecimal Number |
\0 | Null |
\e | Escape character |
\Uhhhhhhhh | Unicode code point. H is a hexadecimal digit |
\uhhhh | Unicode code point below 10000 hex. |
Note: There are some non-standard or invalid escape sequences like \z that needs to be diagnosed, i.e the compiler will print an error message.
Format Specifiers
Format specifiers basically help us work with different types of data types. Format specifiers are generally used during standard input and standard output procedures in C programming Language also referred to as formatted input and formatted output. C does not allow the user to print or input the values straightforward. The programmer must make use of the format specifiers. For example:
scanf("%d",&num); //formatted input for an integer stored in the variable num printf("The value of num is : %d", num); // formatted output for the integer variable num
Format specifiers are preceded by a ‘%’ sign. Here is a list of the format specifiers available in C Programming language.
FORMAT SPECIFIERS | DESCRIPTION |
---|---|
%d or %i | Signed Integer. Supports short, unsigned short, int and long. |
%hi | Signed Integer. Supports short. |
%hu | Unsigned Integer. Supports Unsigned short. |
%l or %ld or %li | Signed Integer. Supports long. |
%lu | Unsigned Integer. Supports unsigned int and unsigned long. |
%lli or %lld | Signed Integer. Supports long long. |
%llu | Unsigned Integer. Supports unsigned long long. |
%o | Octal of an Integer. Supports short, unsigned short, int, unsigned int and long. |
%u | Unsigned integer. Supports unsigned int and unsigned long. |
%x or %X | Hexadecimal of Unsigned Integer. Supports short, unsigned short, int, unsigned int and long. |
%e or %E or %g or %G | Scientific notation of float values. Supports float and double. |
%f | Floating point(float). |
%lf | Floating point(double). |
%Lf | Floating point(long double). |
%c | Character. Supports char and unsigned char. |
%s | String(char *). |
%p | Pointer address to void(void *). |
%n | Null value(prints nothing). |
%% | Prints % character. |
Note: Format specifiers are also known as format strings.
Keep Coding!! Happy Coding!! 🙂
Knowledge is most useful when liberated and shared. Share this to motivate us to keep writing such online tutorials for free and do comment if anything is missing or wrong or you need any kind of help.
Do not forget to share and Subscribe.
Keep Learning… Happy Learning… 🙂