Do you know that C itself doesn’t have any provision for reading data from the input device and displaying data to the output device?
So how are we able to use printf
and scanf
functions?
Let’s explore in this tutorial about C programming input output functions printf and scanf.
Actually, every operating system has its own functions for reading and displaying input-output to and from devices.
Hence, the developers of C compilers write programs that would link the C compiler to those input-output function of the operating system.
We can classify I/O function into two categories:
Console simply means screen and keyboard. There are two types of a console I/O functions:
The major difference is that formatted function allows us to format the input from the keyboard and the output to be displayed on the screen.
We can see that printf( )
and scanf( )
are formatted input/output function.
So let’s discuss printf( )
and scanf( )
:
printf( )
is the standard library function that is used for precise output formatting.
printf( format-control-string, other-arguments );
Format control string in printf( )
function describes the output format which consists of conversion specifiers, precisions, flags, field widths and literal characters.
Each conversion specifier starts with %
sign and ends with a conversion specifier.
We can perform following formatting options with the help of printf( )
:
#include <stdio.h>
int main ()
{
int num = 22;
printf("\nNumber: %d", num);
return 0;
}
Output
Number: 22
Explanation
First of all, printf( )
scans the format control string from left to right. It shows characters onto the screen until it encounters %
or \
.
In the above program, first, it encounters \n
and places the cursor at the beginning of the new line.
Now, Number:
is displayed on the screen.
When it encounters conversion specification in the format control string it picks up the first variable from other arguments such as num
in the above example.
This process continues until the end of format control string.
An integer is a whole number that can be displayed in several formats.
Conversion Specifier |
Description |
---|---|
d | Signed decimal Integer |
i | Signed decimal integer |
o | An unsigned octal integer |
u | An unsigned decimal integer |
x or X | An unsigned hexadecimal integer |
h, I or II | When placed before conversion specifier indicates short, long or long long integer. These are called length modifiers. |
A floating-point value contains decimal point.
Conversion Specifier |
Description |
---|---|
e or E | Floating-point values in exponential notation |
f or F | Floating-point values in fixed notation |
g or G | Floating-point value in either floating-point form or exponential form based on length of the value |
L | Indicates a long double floating-point value if placed before floating-point conversion specifier |
Conversion Specifier |
Description |
---|---|
c | Print individual character |
s | Print string until terminating null character \0 is encountered and requires a pointer to char as an argument |
With the help of field width, we can change the position of data being displayed on the screen.
If the field width is greater than data being displayed, then the data is right justified.
#include <stdio.h>
int main ()
{
printf("%4d\n", 1);
printf("%4d\n", 12);
printf("%4d\n", 123);
printf("%4d\n", 1234);
printf("%4d\n", 123456);
}
Output
1 12 123 1234 123456
Precision has a different meaning when used with different data types.
When used with the integer, it indicates the minimum number of digits to be printed.
Thus, if the value to be printed contains fewer digits than the precision value, zeros are prefixed until the total number of the digit is equivalent to the precision.
When used with the floating-point, it indicates the number of digits to appear after the decimal point.
Example: Use of precision for integer, floating-point, and strings
#include <stdio.h>
int main ()
{
int num1 = 123;
double num2 = 123.45678;
char s[] = "Hello World!!!";
puts("Use of precision for integer");
printf("\t%.4d\n\t%.8d\n\n", num1, num1);
puts("Use of precision for floating-point");
printf("\t%.2f\n\t%.2e\n\n", num2, num2);
puts("Use of precision for string");
printf("\t%.8s\n\t%.12s\n", s, s);
return 0;
}
Output
scanf( )
is the standard library function that is used for precise input formatting.
scanf( format-control-string, other-arguments );
Format control string in scanf( )
function describes the input format and other argument are pointers to list of variables.
Each conversion specifier starts with %
sign and ends with a conversion specifier.
We can perform the following formatting using scanf( )
function
Conversion specifiers for scanf( )
function are same as printf( )
.
Let’s discuss some important features of scanf( )
function.
Reading character using scan set
We can only read certain characters from the input stream using scan set, set of characters enclosed in the square bracket [ ]
that is preceded by a percent %
sign.
The characters from the input stream that matched the character in scan set are stored in the array, otherwise, stops inputting characters when a character that is not stored in scan set is encountered.
On the other hand, if we want to omit certain characters from input stream we should place a care (^)
sign before the scan characters. This is called an inverted scan set.
Inverted scan set is opposite of scan set.
Demonstration of scan set in scanf( )
#include <stdio.h>
int main ()
{
char s[ 9 ];
printf("Enter string: ");
scanf("%9[selz]", s);
printf("The input string is: \"%s\"\n", s);
return 0;
}
Output
Demonstration of Inverted scan set in scanf( )
#include <stdio.h>
int main ()
{
char s[ 10 ];
printf("Enter string: ");
scanf("%9[^selz]", s);
printf("The input string is(Inverted scan set): \"%s\"\n", s);
return 0;
}
Output