Format Specifiers in C
Format specifiers can be defined as operators which are used to print data that are stored in the variables using the printf() function and taking input data to store in a variable using scanf() function.
Simply, we can say that format specifiers are used for input and output purpose because we cannot print the value stored in the variable directly without using the format specifiers available in C. It is a way to do this job by using some format specifiers.
List of C Format Specifiers
Format Specifiers | Description |
---|---|
%d | Integer Format Specifier It is used to print the signed integer value. |
%f | Float Format Specifier It is used to print decimal floating point values. |
%s | String Format Specifier It is used to print strings. |
%c | Character Format Specifier It is used to print single unsigned character. |
%u | Unsigned Int Format Specifier It is used to print unsigned( only positive ) integer value. |
%ld | Long Int Format Specifier It is used to print long-singed integer value. |
%o | Octal Unsigned Int Format Specifier It is used to print the octal unsigned integer value. |
Integer Format Specifier '%d'
It is used to print the signed integer value. Here, signed integer means a variable holds value with their sign(+ve or -ve). %d specifier is used in printf() and scanf() function to print a value and taking input by users respectively.
Let's understand Integer format specifier by the help of an example.
hello world
Float Format Specifier '%f'
It is used to print fractional values. %f format specifier indicates that the value you want to print or store in the variable, is floating type value. C compiler can understand that float type of data is in a variable during printing data of the variable.
Let's take an example of Float format specifier.
hello world
String Format Specifier '%s'
This specifier is used to print collection of character (known as string). %s format specifier is used in printf() function to print string that is stored in array variable.
hello world
Character Format Specifier '%c'
It is used to print single character stored in the variable. %c format specifier is used in the both functions(printf & scanf). To print a character we use %c format specifier in the printf() function and in the scanf() function, it is used to take a character from keyboard as input.
Let's see an example to understand it.
hello world
Unsigned Int Format Specifier '%u'
It is used to print the unsigned integer value. It means a variable can hold only positive value. To print unsigned integer value, we use %u format specifier in the printf() function.
To illustrate this, here is a suitable example.
hello world
Long Int Format Specifier '%ld'