
- C Library - Home
- C Library - <assert.h>
- C Library - <complex.h>
- C Library - <ctype.h>
- C Library - <errno.h>
- C Library - <fenv.h>
- C Library - <float.h>
- C Library - <inttypes.h>
- C Library - <iso646.h>
- C Library - <limits.h>
- C Library - <locale.h>
- C Library - <math.h>
- C Library - <setjmp.h>
- C Library - <signal.h>
- C Library - <stdalign.h>
- C Library - <stdarg.h>
- C Library - <stdbool.h>
- C Library - <stddef.h>
- C Library - <stdio.h>
- C Library - <stdlib.h>
- C Library - <string.h>
- C Library - <tgmath.h>
- C Library - <time.h>
- C Library - <wctype.h>
- C Programming Resources
- C Programming - Tutorial
- C - Useful Resources
C Library - scanf() function
The C library function scanf(const char *format, ...) reads formatted input from standard input stream (typically the keyboard).
Syntax
Following is the C library syntax of the scanf() function −
int scanf(const char *format, ...);
Parameters
This function accepts two parameter −
- format: This is a C string that contains a format string. The format string specifies the type of input expected and is composed of conversion specifications starting with a % character.
- ... :These are the additional arguments corresponding to the format specifiers. Each argument must be a pointer to a variable where the parsed input will be stored.
Sr.No. | Argument & Description |
---|---|
1 |
* 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. |
2 |
width This specifies the maximum number of characters to be read in the current reading operation. |
3 |
modifiers 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) |
4 |
type 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 Value
The 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 Specifiers
Some common format specifiers include:
- %d for integers
- %f for floating-point numbers
- %c for characters
- %s for strings
Example 1: Reading an Integer
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: 23
Example 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