The C library function scanf(const char *format, ...) reads formatted input from standard input stream (typically the keyboard).
SyntaxFollowing is the C library syntax of the scanf() function −
int scanf(const char *format, ...);Parameters
This function accepts two parameter −
*
This is an optional starting asterisk indicates that the data is to be read from the stream but ignored, i.e. it is not stored in the corresponding argument.
2width
This specifies the maximum number of characters to be read in the current reading operation.
3modifiers
Specifies a size different from int (in the case of d, i and n), unsigned int (in the case of o, u and x) or float (in the case of e, f and g) for the data pointed by the corresponding additional argument: h : short int (for d, i and n), or unsigned short int (for o, u and x) l : long int (for d, i and n), or unsigned long int (for o, u and x), or double (for e, f and g) L : long double (for e, f and g)
4type
A character specifying the type of data to be read and how it is expected to be read. See next table.
type Qualifying Input Type of argument c Single character: Reads the next character. If a width different from 1 is specified, the function reads width characters and stores them in the successive locations of the array passed as argument. No null character is appended at the end. char * d Decimal integer: Number optionally preceded with a + or - sign int * e, E, f, g, G Floating point: Decimal number containing a decimal point, optionally preceded by a + or - sign and optionally followed by the e or E character and a decimal number. Two examples of valid entries are -732.103 and 7.12e4 float * o Octal Integer: int * s String of characters. This will read subsequent characters until a whitespace is found (whitespace characters are considered to be blank, newline and tab). char * u Unsigned decimal integer. unsigned int * x, X Hexadecimal Integer int * Return ValueThe scanf function returns an integer value which indicates the number of input items successfully matched and assigned. If the input does not match the format specifiers, or if the end of the input stream is reached before any matches are made, scanf returns EOF.
Format SpecifiersSome common format specifiers include:
Here, we prompts the user to enter an integer, reads the input using scanf, and then prints the integer if it was successfully read.
Below is the illustration of the C library scanf() function.
#include <stdio.h> int main() { int num; printf("Enter an integer: "); int result = scanf("%d", &num); if (result == 1) { printf("You entered: %d\n", num); } else { printf("Failed to read an integer.\n"); } return 0; }Output
The above code produces following result−
Enter an integer: 23 You entered: 23Example 2: Reading Multiple Values
This example reads an integer, a floating-point number, and a character from the user, and prints them if all three values are successfully read.
#include <stdio.h> int main() { int age; float height; char initial; printf("Enter your age, height (in meters), and first initial: "); int result = scanf("%d %f %c", &age, &height, &initial); if (result == 3) { printf("You entered: Age = %d, Height = %.2f, Initial = %c\n", age, height, initial); } else { printf("Failed to read all values.\n"); } return 0; }Output
After execution of above code, we get the following result
Enter your age, height (in meters), and first initial: 26 180 R You entered: Age = 26, Height = 180.00, Initial = R
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4