Check The Programming Section
The formatted input functions reads data from input devices such as keyboard. The formatted input functions support all basic data types such as, int, char, float, double, derived data types and user defined types. The following list of formatted input functions are defined in stdio (in C) or cstdio (in C++) header file:
scanf( ) - takes data input from standard input
fscanf( ) - reads data from a file
sscanf( ) - takes input from designated character array
Standard input functions required a formatted specifier to read data from file or stdin or character buffer like formatted output functions such as printf( ). Required format specifiers are already listed here.
The scanf( ) is a formatted function is used to read any type data value and assigned to a previously declared variable. The scanf( ) function also requires format specifiers listed in the above table like printf( ). These format specifiers are required to identify the data to be read during the execution of the source code. The prototype of the scanf( ) is given below:
int scanf (format_string, list_of_variable_addresses);
Details of Arguments
format_string - same as printf( ) have only format specifiers and number of format specifiers must equal to the number variable addresses
list_of_variable_addresses - like printf( ) instead of expressions, variable addresses need to pass. To pass an address of a variable the address of operator & will be preceded by the variable name.
Return Type
int - like printf( ) and sprintf( ) scanf also returns an integer. But instead of number of characters scanf( ) returns a number of accepted inputs during the execution of the program. This number is equal to the number of variable addresses passed as second arguments onwards.
Taking Input
To take input a variable of type int, at first we need to declare a variable of type int. The name of the variable must satisfy the rules for constructing variable names discussed in Chapter 2. For example,
#include<stdio.h>
int main(){
int num;
scanf(“%d”, &num);
}
Note: If one scanf( ) function is used to take input for multiple variables then we must use comma (,) as a separator in between the list of variable addresses. For example,
int main(){
int a, b;
scanf(“%d %d”,&a, &b);
}
Note: while giving input multiple values at run-time we must give a space between two values, else both numbers are treated as a single number. When multiple values are taking as input using one scanf( ) function then scanf( ) supports two ways of taking the input from an user as follows:
space separated (by default)
comma separated
Comma Separated Values
int main(){
int a, b;
scanf(“%d,%d”,&a, &b);
}
Note: inside the scanf( ) function comma is used as a separator in between two format specifiers.
Displaying a message before taking input
To display a message before taking an input will help an user to input a correct value, which will satisfy the requirement of the scanf( ). For example,
#include<stdio.h>
int main(){
char ch;
printf(“Enter any uppercase character: ”);
scanf(“%c”, &ch);
printf(“The variable %c is given as input”,ch);
}
Taking different types of values as input
In C/C++, scanf( ) function also allows us to take different types of values using one scanf( ) statement. For that, we need to pass required format specifiers inside the format string. For example,
#include<stdio.h>
int main(){
int num1;
char ch;
float f;
scanf("%d %c %f", &num1, &ch, &f);
printf("%d %c %f", num1, ch, f);
}
sscanf( ) is used to take input from a character buffer, instead of standard in. The prototype of the function is as follows:
int sscanf (const char *, format_string, list_of_variable_address);
Details of the Arguments
const char * - a constant character buffer, in which datas are present and will be used to read data from const char *.
format_string - same as scanf( ) function
list_of_variable_address - comma separated variables addresses will be passed, in which data will be written from const char *.
Return Type
int - like scanf( ) function, sscanf( ) also returns an integer value as number of inputs are taken from char *.
Taking Input
Before taking input, variables must be declared as specified data type according to the data type present in const char buffer. For example,
#include<stdio.h>
int main( ){
char buffer[30] = "Hello World"; //line1
char str1[10], str2[10]; //line2
sscanf(buffer, "%s %s", str1, str2); //line3
printf("%s %s", str1, str2);
}
The char array buffer is filled with two space separated words at line1 (as Hello World). Two other char arrays as str1 and str2 are declared in line2. At line3, sscanf( ) function is used to read data from buffer and write it to str1 and str2. In this example, a string is read word by word from buffer and write it to two different array str1 and str2.
Reading the complete string into one variable
We can read the complete information into one char array and to do that, we need to change the escape sequence character of %s format specifier. The default escape sequence sequence character for %s is a white space. It means using sscanf( ) and %s format specifier we can read a single word only. To change the default escape sequence for %s we need to use %s as %[^\0]s. The ‘\0’ represents the null character and character arrays in C/C++ are the null terminated string. It means till the encounter of a null character sscanf( ) function will read the complete string into one variable. For example,
#include<stdio.h>
int main(){
char buffer[30] = "Hello World";
char str1[30];
sscanf(buffer,"%[^\0]s", str1);
printf("%s",str1);
}
Reading two different data values
It is also possible to read two different types of data values using one sscanf( ) function and to do that, the required format specifier needs to be passed in format_string like scanf( ). For example,
#include<stdio.h>
int main(){
int num;
char info[20];
char buffer[30] = "Age 30";
sscanf(buffer, "%s %d",info, &num);
printf("%s %d",info, num);
}
It is used to read data from a file stream using a formatted string with a list of variable addresses like scanf(). The prototype of the function is as follows:
int fscanf(FILE *, format_string, list_of_variable_address);
FILE * - pointer to a file object and points to a file stream
format_string - same as scanf( )
list_of_variable_addresses - same as scanf( )
int - returns an integer value as number of input items successfully reads from a file stream and assigned to number of matching variables.
Before reading data from a file stream, the file needs to be opened in reading mode ( r ). To do that, a file must be present at the directory of the project or in a specified location. For example, suppose in a text file namely abc.txt a line We are in 2020 is written. Let's read this data into respective variables.
#include<stdio.h>
#include<stdlib.h>
int main( ){
FILE *ptr;
int year;
char str1[10], str2[10], str3[10];
ptr = fopen("abc.txt","r");
fscanf(ptr, "%s %s %s %d",str1, str2, str3, &year);
printf("%s %s %s %d", str1, str2, str3, year);
}