The scanf() function is defined in the stdio.h header file. It helps to input the data. It reads formatted input from stdin stream.
int scanf(const char *format, ...); #where format can be a integer, character, string, float
The scanf() function takes a format string as a parameter. It can contain whitespace character, Non-whitespace character and Format specifiers.A format specifier will be like [=%[*][width][modifiers]cype=].
* - it indicates that the data is to be read from the stream but ignored.
width - It specifies the maximum number of characters to be read in the current reading operation.
modifiers - int, unsigned int, or float for the data pointed by the corresponding additional argument: h: short int, or unsigned short int,l: long int, or unsigned long int, or double L: long double.
type - A character specifying the type of data to be read and how it is expected to be read.
Parameter | Description | Required / Optional |
---|---|---|
format | the string that contains the text to be written to the stdout stream | Required |
additional arguments | containing one value to be inserted instead of each %-tag specified in the format parameter | Optional |
The return value of scanf() function is a total number of reading characters in success and an EOF on failure.
Input | Return Value |
---|---|
If successful, | total number of characters |
If reading error or EOF reached | EOF is returned |
#include <stdio.h>
int main()
{
char st1[25], st2[25];
printf("Enter Place: ");
scanf("%s", st1);
printf("Enter your name: ");
scanf("%s", st2);
printf("Entered Place is: %s\n", st1);
printf("Entered Name is:%s", st2);
return(0);
}
Output:
Enter Place:London Enter your name:Albert Entered Place is:London Entered Name is:Albert
#include <stdio.h>
int main (){
int x;
printf("Enter the value of x:");
scanf("%d", &x);
printf("The value of x is: %d", x);
return 0;
}
Output:
Enter the value of x: 12 The value of x is: 12