Input means to provide the program with some data to be used in the program Output means to display data on screen or write the data to a printer or a file. printf() and scanf() functions are inbuilt library functions in C programming language which are generally used for Input and Output
When we say Input, it means to feed some data into a program. An input can be given in the form of a file or from the command line. C programming provides a set of built-in functions to read the given input and feed it to the program as per requirement.
When we say Output, it means to display some data on screen, printer, or in any file. C programming provides a set of built-in functions to output the data on the computer screen as well as to save it in text or binary files.
The standard input-output header file, named stdio.h contains the definition of the functions printf() and scanf(), which are used to display output on screen and to take input from user respectively.
Note:
C language is case sensitive. For example, printf() and scanf() are different from Printf() and Scanf(). All characters in printf() and scanf() functions must be in lower case.
#include<stdio.h>
void main()
{
// defining a variable
int i;
/*
displaying message on the screen
asking the user to input a value
*/
printf("Please enter a value...");
/*
reading the value entered by the user
*/
scanf("%d", &i);
/*
displaying the number as output
*/
printf( "\nYou entered: %d", i);
}
You must be wondering what is the purpose of %d and & inside the scanf() or printf() functions. It is known as format string and this informs the scanf() function, what type of input to expect and in printf() it is used to give a heads up to the compiler, what type of output to expect.
The format specifier %d is used in scanf() statement. So that, the value entered is received as an integer.
Ampersand is used before variable name "i" in scanf() statement as &i.
It is just like in a pointer which is used to point to the variable
Format String | Meaning |
---|---|
%d | Scan or print an integer as signed decimal number |
%f | Scan or print a floating point number |
%c | To scan or print a character |
%s | To scan or print a character string. The scanning ends at whitespace. |