Formatted I/O functions are used to take various inputs from the user and display multiple outputs to the user. These types of I/O functions can help to display the output to the user in different formats using the format specifiers. These I/O supports all data types like int, float, char, and many more.
Why they are called formatted I/O?
These functions are called formatted I/O functions because we can use format specifiers in these functions and hence, we can format these functions according to our needs.
The following formatted I/O functions will be discussed in this section-
- printf()
- scanf()
- sprintf()
- sscanf()
1. printf()
The printf() function is the most used function in the C language. This function is defined in the stdio.h header file and is used to show output on the console (standard output).
This function is used to print a simple text sentence or value of any variable which can be of int, char, float, or any other datatype.
printf(“Hi”);
and the program will print the content of the string to the screen.
You can print the value of a variable, and it’s a bit tricky because you need to add a special character, a placeholder, which changes depending on the type of the variable. For example we use %d for a signed decimal integer digit:
Int age = 25;
Printf(“Myage is %d”, age);
We can print more than one variable by using commas:
Int age_yesterday = 36;
Int age_today = 37;
Printf(“ Yesterday my age was %d and today is %d”, age_yesterday, age_today);
There are other format specifiers like %d:
- %c for a char
- %s for a string
- %f for floating point numbers
- %p for pointers
and many more.
We can use escape characters in printf(), like \n which we can use to make the output create a new line.
2. scanf()
scanf() function is used to read/input values of variables using the standard input device such as keyboard. This function is used to get a value from the user running the program, from the command line.
We must first define a variable that will hold the value we get from the input:
Int age;
Then we call scanf() with 2 arguments: the format (type) of the variable, and the address of the variable:
scanf(“%d”, &age);
If we want to get a string as input, remember that a string name is a pointer to the first character, so you don’t need the & character before it:
char name[20];
scanf(“%s”, name);
Here’s a little program that uses both printf() and scanf():
#include <stdio.h>
int main(void) {
char name[20];
printf(“Enter your name: “);
scanf(“%s”, name);
printf(“you entered %s”, name);
}
3. sprintf()
sprintf stands for “string print”. This function is similar to printf() function but this function prints the string into a character array instead of printing it on the console screen.
Syntax:
sprintf(array_name, “format specifier”, variable_name);
4. sscanf():
sscanf stands for “string scanf”. This function is similar to scanf() function but this function reads data from the string or character array instead of the console screen.
Syntax:
sscanf(array_name, “format specifier”, &variable_name);