Scanf function in C which allows the programmer to accept input from the standard input device (keyboard) and stores them in variables. The scanf is similar to printf function. Instead of printing data on the output device it reads data entered from the input device.
- The scanf function reads input from the standard input device into one or more variables
Example: scanf(“%lf”, &miles);
Reads a real number that (by default) the user has typed in into the variable miles
- Each variable must be preceded by the ampersand (&) address-of operator
- We can read values into multiple variables with a single scanf as long as we have corresponding format strings for each variable
Example: scanf(“%c%c%c”, &letter_1, &letter_2, &letter_3);
- The user must press the return or enter key to cause the value(s) to be read
- We can read values into multiple variables with a single scanf as long as we have correpsonding format strings for each variable
Example: scanf(“%c%c%c”, &letter_1, &letter_2, &letter_3);
- The user must press the return or enter key to cause the value(s) to be read
- For numeric values (associated with a %lf or %d placeholder) blank spaces are skipped
- For character values (associated with a %c placeholder) blank spaces are not skipped (since a space is a character!)
Example: scanf(“%c%c%c”,&x,&y,&z);
input: cat
result:catinput: c a t
result:c a tExample: scanf(“%d%d”,&n1,&n2);
input: 1 2 3
result:123
The below program illustrates the use of Scanf function,
#include<stdio.h> main() /* program which illustrate the scanf function */ { int num; printf("Input an Integer Number \n"); scanf("%d", &num); printf("The number you typed was %d\n", num); }
Sample Program Output
Input an Integer Number
50
The number you typed is 50
An integer called num is declared. A prompt to enter in a number is then printed on the screen using the statement
printf("Input an Integer Number \n");
The scanf routine, which accepts the response, has two arguments. The first (“%d”) specifies what type of data type is required (ie char, int, or float).
FORMATTERS FOR scanf()
Litst of Characters used after the % character, in a scanf argument.
Modifer | Meaning |
d | read a decimal integer |
o | read an octal value |
x | read a hexadecimal value |
h | read a short integer |
l | read a long integer |
f | read a float value |
e | read a double value |
c | read a single character |
s | read a sequence of characters, stop reading when an enter key or whitespace character [tab or space] |
[…] | Read a character string. The characters inside the brackets indicate the allow-able characters that are to be contained in the string. If any other character is typed, the string is terminated. If the first characteris a ^, the remaining characters inside the brackets indicate that typing them will terminate the string. |
* | this is used to skip input fields |
The second argument (&num) specifies the variable into which the typed response will be placed. In this case the response will be placed into the memory location associated with the variable num.
This explains the special significance of the & symbol (which means the address of).
Example 1 : Use of he scanf() function to read in user input
/* Use of the scanf() function to read in user input Code Credit : Richard Chang */ #include<stdio.h> main() { int n ; double x ; char name[101] ; // this is a string or char array printf ("Enter an integer value for n: ") ; scanf("%d", &n) ; // don't forget the ampersand printf ("Enter a floating point value for x: ") ; scanf("%lf", &x) ; // double requires lf, not f printf ("Enter your first name (no spaces): ") ; scanf("%100s", name) ; // arrays do not need the & printf ("Hello %s, you typed in %d for n and %f for x.\n", name, n, x) ; }
Example 2 : Use of the scanf() function to read in user input What happens if you forget the &?
/* Test use of the scanf() function to read in user input What happens if you forget the &? */ #include<stdio.h> main() { int n ; double x ; char name[101] ; // this is a string or char array printf ("Enter an integer value for n: ") ; scanf("%d", &n) ; // don't forget the ampersand printf ("Enter a floating point value for x: ") ; scanf("%lf", x) ; // double requires lf, not f printf ("Enter your first name (no spaces): ") ; scanf("%100s", name) ; printf ("Hello %s, you typed in %d for n and %f for x.\n", name, n, x) ; }
Example 3 : Use of the scanf() function to read in user input. What happens if you use %f instead of %lf?
/* File: scanf3.c Test use of the scanf() function to read in user input What happens if you use %f instead of %lf? */ #include<stdio.h> main() { int n ; double x ; char name[101] ; // this is a string or char array printf ("Enter an integer value for n: ") ; scanf("%d", &n) ; // don't forget the ampersand printf ("Enter a floating point value for x: ") ; scanf("%f", &x) ; // double requires lf, not f printf ("Enter your first name (no spaces): ") ; scanf("%100s", name) ; printf ("Hello %s, you typed in %d for n and %f for x.\n", name, n, x) ; }
Example 4 : Use of the scanf() function to read in user input
What happens if your string is too short??
/* Test use of the scanf() function to read in user input What happens if your string is too short?? */ #include<stdio.h> main() { int n ; double x ; char name[4] ; // this string only holds 4 characters printf ("Enter an integer value for n: ") ; scanf("%d", &n) ; // don't forget the ampersand printf ("Enter a floating point value for x: ") ; scanf("%lf", &x) ; // double requires lf, not f printf ("Enter your first name (no spaces): ") ; scanf("%100s", name) ; printf ("Hello %s, you typed in %d for n and %f for x.\n", name, n, x) ; }