In C language, two types of Input / Output Statements are available, and all input and output operations are carried out through function calls. Several functions are available for input / output operations in ‘C‘. These functions are known as the standard I/O library.
Formatted Input / Output statements
formatted Input / Output refers to input and output, that have been arranged in a particular format.
The input and output functions in C are built around the concept of a set of standard data streams being connected from each executing program to the basic input/output devices.
Example: SMMS 2939
This line contains two parts of data, that are arranged in a format, such data can be read to the format of its appearance, as the first data should be read into a variable char, the second into an int.
Such operation can be made possible in C language by the formatted Input / Output statements.
The scanf() function
Input data can be entered into the computer using the standard input C library function called scanf(). This function is used to enter any combination of input.
The scanf() function is used to read information from the standard input device (keyword), scanf() function starts with a string argument and may contain additional arguments. Any additional arguments must be pointers.
Syntax: scanf(“control string”, &var1, &var2,…,&varn);
Description: The Control string consists of character groups.
Example: int n;
Control strings in C language
It is the type of data the user going to accept via the input statements, this can be formatted and always preceded with a ‘%’ sign.
The scanf control string or placeholder consists of % at the beginning and type indicator at the end. Apart from that, it can have *, a maximum field width indicator, and a type indicator modified.
If input consists of 6 values and we want to ignore the middle 3 values, we can write. scanf(“%d%d%*d%*d%*d%d”, &i, &j, &k);
so, if our input is 10 20 30 40 50 60. It will get values 10 to i, 20 to j, 30 to k.
The * is used to suppress input. Example: %*d
Rules for writing scanf() function
- The control string must be preceded with a (%) sign and must be within quotations.
- If there is a number of input data items, items must be separated by commas and must be preceded with a (&) sign except for string input.
- The control string and the variables going to input should match each other.
- It must have termination with semicolons.
- The scanf() reads the data values until blank space in numeric input or a maximum number of characters has been read, or an error is detected.
Program to get input values using scanf ()function.
Enter the three values : 67 98 11 The input values 67 98
Explanation: This program displays the number of values inputted to the specified variable using scanf() statement
The Printf() function in c language
Output data or the result of an operation can be displayed from the computer to a standard output device using the library function printf(). This function is used to output any combination of data.
It is similar to the input function scanf() except that it displays data rather than input.
Syntax: printf(“control string”, var1,var2,…..varn);
description: var1, var2, varn are the arguments or variables from which the data is going to output. Here the variables need not be preceded with the ‘&’ sign.
Example:printf(“Result is…%d”, n);
field width plays an important role in printf statements while producing reports in a formatted manner. Field width indicates the least number of columns that will be to the output.
Example: i = 67, its field width is %4d.
So, the four columns are allocated for i and 2 blanks are added on the left side of the value of i.
The output looks like
– (minus)indicates the output is left justified. Example: printf(“%d-10.4d\n”,67);
+ indicates the number is printed using a sign character either (+ or -)
printf(“%+d\n”, -25); It prints the value -25
printf(“%d+d\n”, +25); It prints the value +25
Rules for writing printf() function in c language
- Place appropriate headings in the output.
- The variable must be separated by commas, and need not be preceded with ‘&’ sign.
- The control string and the variables must match in their order.
- The control string and the variables must be quotations and there we can also use any other text to print with data.
- Provide blank space in between the numbers for better readability.
- Print special messages wherever required in the output.
Program to demonstrate printf() function.