In the previous chapter of this C tutorial about input/output statement, we've learnt about format specifiers being used as part of printf
and scanf
function. In this chapter let's learn about the format specifiers in detail.
We've seen that, format specifiers have a leading "%" character followed by a conversion character (a letter). Different conversion characters are used for different data types.
Conversion Characters Used for Each Data Type
Below table specifies the conversion character associated with each data type in C language.
Format Specifier | Associated Datatype | Description |
---|---|---|
%c |
char | Used to format single character |
%d or %i |
int | Used to format a signed integer |
%u |
int | Used to format an unsigned integer in decimal form |
%o |
int | Used to format an unsigned integer in octal form |
%x or %X |
int | Used to format an unsigned integer in hex form |
%h |
int | Used to format an short integer |
%e or %E |
float or double | Used to format a float or double in exponential form |
%f |
float or double | Used to format a float or double in decimal format |
%s |
char[] | Used to format a string/character sequence |
Example of Print Format Specfiers
Now that we know how to print any data type, let's take a look at a simple example.
#include <stdio.h> int main() { int i = 30; char c = 'z'; float f = 5.67; char s[] = "This is a string"; printf("i=%d, c=%c, f=%f, s=%s", i,c,f,s); return 0; } Output: i=30, c=z, f=5.670000, s=This is a string
In this program, we've got some familiar variable declaration; one int
, one char
one float
and one string (char[]
). After declaring and assigning values to the variables, we invoked a printf
statement which print contents between two double inverted commas to the standard output or the screen. The contents are written in the screen using print format specifiers, which replace the values of the variables that appears after closing '"'.
Here %d
is replaced by decimal value of the variable 'i' which is 30, %c
is replaced by the character stored in variable 'c', %f
is replaced by the floating point value with variable 'f' and %s
is replaced by the string represented by array 's'. Note the order of the print format specifiers are same as the order of variables appearing in the printf
statement. The order of declaration does not matter as long as you declare the variables before you print them.
Also Read - Top C Interview Questions and Answers for Freshers & Advanced C Interview Questions and Answers
Scanf Format Specifier
The above mapping of data types and corresponding format specifiers is also applicable to scanf
function. The %c
format specifier is used to read a single character from the standard input, %s
specifier allows to read a string with a whitespace character as terminating character (space, line feed, carriage return etc.) and similar with other datatypes.
There is a difference in the use of gets
and scanf
with %s
specifier. If you use scanf,
it will read a string from input until a whitespace character is read. But gets
will read a string until a newline character (\n
) is reached. So if you use scanf("%s", line)
, where the line is a character array, and your input is a string "Nice C tutorial"
, after execution of this this statement you will get string "Nice"
in the variable line
. Because "Nice"
is followed by a whitespace character (blank space). But using gets
will populate the variable line
with the whole string.
We can also specify maximum field width in the scanf
. It is done by adding a number after % and before the specifier to limit the width of the input. Let's take a look at it with the help of an example,
#include <stdio.h> int main(void) { int a; printf("Enter number: "); scanf("%5d", &a); printf("Formatted number is: %d", a); return 0; } Output: Enter number: 123456 Formatted number is: 12345
In the above program, the format specifier used with scanf
statement is %5d
. This means, only the first 5 characters will be read into the variable. So the input '123456' is trimmed down to '12345'.
This can be applied to printf
as well. But the result is quite different. If a variable has the value '123' and you use %5d
in a printf
statement, you will get 5 characters as output with leading two blank space and then three digit, but if you use %2d
it will print all 3 digits.
Also Read - Top Embedded C Interview Questions and Answers & C++ Interview Questions and Answers
Assignment Suppression Character
Sometimes an unnecessary character in the input can lead to wrong value population in the variables when using scanf
. For example, if we are trying to get an hour and minute of the day in hh:mm format using scanf
. We have written our scanf
like
scanf("%d%d", &hour, &minute)
But we overlooked the character ":" in our input. It will lead to the assignment of hourly value to variable hour
variable and ":" to minutes
, which will lead to error. To solve this problem, we can use assignment suppression character. The assignment suppression character is %
sign followed by an asterisk (*) and followed by appropriate specifier. Then we can use it like -
scanf("%d%*c%d", &hour, &temp, &minute);
Similar problem can happen when we want to read two characters from input with statement like scanf("%c%c", &first, &second)
, and the input has a blank space in between two characters. It will lead to population of variable first
with first character and second
with blank space. We can avoid this by using %1s
in scanf like scanf("%c%1s", &first, &second)
. It will populate variables first
and second
with proper inputs. There is another alternative too, using scanf("%c %c", &first, &second)
also gets the job done.