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 SEQUENCESDESCRIPTION
\aalarm or beep or bell
 \bbackspace
\fForm feed
\nNew Line
\rCarraige Return (reset the device’s position to the beginning of a line)
\tHorizontal Tab
\vVertical Tab
 \\Backslash
 \’Single Quote
 \”Double Quote
 \?Question Mark(used to avoid trigraphs)
 \nnnInterpreted as an Octal Number
\xhhInterpreted as a Hexadecimal Number
\0Null
\eEscape character
\UhhhhhhhhUnicode code point. H is a hexadecimal digit
\uhhhhUnicode 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 SPECIFIERSDESCRIPTION
%d or %iSigned Integer. Supports short, unsigned short,  int and long.
%hiSigned Integer. Supports short.
%huUnsigned Integer. Supports Unsigned short.
%l or %ld or %liSigned Integer. Supports long.
%luUnsigned Integer. Supports unsigned int and unsigned long.
%lli or %lldSigned Integer. Supports long long.
%lluUnsigned Integer. Supports unsigned long long.
%oOctal of an Integer. Supports short, unsigned short, int, unsigned int and long.
%uUnsigned integer. Supports unsigned int and unsigned long.
%x or %XHexadecimal of Unsigned Integer. Supports short, unsigned short, int, unsigned int and long.
%e or %E or %g or %GScientific notation of float values. Supports float and double.
 %fFloating point(float).
%lfFloating point(double).
%LfFloating point(long double).
%cCharacter. Supports char and unsigned char.
%sString(char *).
 %p Pointer address to void(void *).
%nNull 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… 🙂 

Recommended -