Basic Input/Output in C | printf and scanf
In many of the previous chapters, we have seen the inclusion of libraries (like; stdio.h) and use of printf function. printf is a basic I/O function in C. In this chapter, we will learn about the Formatted I/O functions in C.
About stdio.h
The standard input output library (stdio.h) is a standard library of the C system that provides a set of functions for input and output. The functions in this library are:
The stdio.h header file has the definitions of the functions like scanf(), printf(), etc.. To use this library, you need to include the header file stdio.h in your program.
- scanf()
- printf()
- getc()
- putc()
- fopen()
- fclose()
The stdio.h header file has the definitions of the functions like scanf(), printf(), etc.. To use this library, you need to include the header file stdio.h in your program.
#include <stdio.h>
About printf
The printf() function, in C, prints the value passed as the parameter to it, on the console screen.
The syntax of the printf() function is:
The syntax of the printf() function is:
printf ("%X", variableOfTypeX);
// Here, %X is the format specifier which tells the compiler the type of the data to store in the variable.
// Here, %X is the format specifier which tells the compiler the type of the data to store in the variable.
Let's take an example
C
#include <stdio.h>
int main()
{
int age = 18;
printf("%d", age);
return 0;
}
int main()
{
int age = 18;
printf("%d", age);
return 0;
}
Output
18
Explanation
In the above example, a int variable age is declared and initialized to 18. The printf() function is used to output the value of the variable age on the console screen.
Here, %d is the format specifier for the integer data type.
Here, %d is the format specifier for the integer data type.
About scanf
The scanf() function, in C, reads the value from the console as per the type specified in the format specifier.
The syntax of the scanf() function is:
In this chapter, we have covered only the basic input/output functions. There are many more functions that can be used to input/output data, which we will be covering in the coming chapters.
The syntax of the scanf() function is:
scanf ("%X","&variableOfTypeX");
// Here, %X is the format specifier which tells the compiler the type of the data is in the variable.
// Also, & is the address operator which gives the address of the variable to the scanf function.
// Here, %X is the format specifier which tells the compiler the type of the data is in the variable.
// Also, & is the address operator which gives the address of the variable to the scanf function.
Let's take an example
C
#include <stdio.h>
int main()
{
int age;
printf("Enter your age : ");
scanf("%d", &age);
printf("%d", age);
return 0;
}
int main()
{
int age;
printf("Enter your age : ");
scanf("%d", &age);
printf("%d", age);
return 0;
}
Output
Enter your age : 17
17
17
Explanation
In the above example, an int variable age is declared and using the scanf function, an integer input is expected. After the integer input is given, then it is stored in the address of the age variable (&age). Which means, when the variable age is printed, the input is then printed on the console screen.
Here, %d is the format specifier for the integer data type.
Here, %d is the format specifier for the integer data type.
Format Specifiers
The format specifier is used to tell the compiler the type of the data is in the variable, or the type of the data the user is expected to input. The format specifier is used in the scanf() and printf() functions. Some of the format specifiers are:
Format Specifier | Description |
---|---|
%d | Integer |
%f | Floating point |
%c | Character |
%s | String |
%x | Hexadecimal |
%lf | Double |
%ld | Long |
%hd | Short |
%lld | Long long |
%llu | Unsigned long long |
%lu | Unsigned long |
%hu | Unsigned short |
%hhu | Unsigned char |
%e or %E | Scientific notation |
%% | Used to print a single % |
Examples using different format specifiers
C
#include <stdio.h>
int main()
{
int age;
char favouriteLetter;
float g;
double PI;
printf("Enter your age : ");
scanf("%d", &age);
printf("Enter your favourite letter : ");
scanf(" %c", &favouriteLetter);
printf("Enter the value of g : ");
scanf("%f", &g);
printf("Enter the value of PI : ");
scanf("%lf", &PI);
printf("Your age is %d, ", age);
printf("your favourite English Letter is %c, ", favouriteLetter);
printf("the value of g is %f and, ", g);
printf("the value of PI is %lf.", PI);
return 0;
}
int main()
{
int age;
char favouriteLetter;
float g;
double PI;
printf("Enter your age : ");
scanf("%d", &age);
printf("Enter your favourite letter : ");
scanf(" %c", &favouriteLetter);
printf("Enter the value of g : ");
scanf("%f", &g);
printf("Enter the value of PI : ");
scanf("%lf", &PI);
printf("Your age is %d, ", age);
printf("your favourite English Letter is %c, ", favouriteLetter);
printf("the value of g is %f and, ", g);
printf("the value of PI is %lf.", PI);
return 0;
}
Output
Enter your age : 18
Enter your favourite letter : D
Enter the value of g : 9.18
Enter the value of PI : 3.1415
Your age is 18, your favourite English Letter is D, the value of g is 9.180000 and, the value of PI is 3.141500.
Enter your favourite letter : D
Enter the value of g : 9.18
Enter the value of PI : 3.1415
Your age is 18, your favourite English Letter is D, the value of g is 9.180000 and, the value of PI is 3.141500.
Additional Information
Between the % and the X in %X, there are options that can be used. They are called flags.
Flag | Description |
---|---|
- | The value is padded with spaces and is left justified in the field. Right justification is the default. |
+ | The plus sign forces to display the sign of the number, even if the number is positive. By default, only the minus sign is displayed if the number is negative. |
0 | The value is padded with zeros from the left. |
Any number | It specifies the minimum field width for the value. If the value is shorter than the field width, the field is padded with spaces from the left. |
. | A period is used to seperate the field width and the precision. It is used in the case of floating point numbers, to specify the number of digits after the decimal point. |
Examples using different format specifiers
C
#include <stdio.h>
int main()
{
int age = 18;
float PI = 3.1415;
printf("%10d\n", age);
printf("%010d\n", age);
printf("%-d\n", age);
printf("%-10d\n", age);
printf("%+d\n", age);
printf("%.2f\n", PI);
return 0;
}
int main()
{
int age = 18;
float PI = 3.1415;
printf("%10d\n", age);
printf("%010d\n", age);
printf("%-d\n", age);
printf("%-10d\n", age);
printf("%+d\n", age);
printf("%.2f\n", PI);
return 0;
}
Output
18
0000000018
18
18
+18
3.14
0000000018
18
18
+18
3.14