C Function Scanf
# C Library Function - scanf()
[ C Standard Library - ](#)
## Description
The C library function **int scanf(const char *format, ...)** reads formatted input from stdin.
## Declaration
Here is the declaration for the scanf() function.
int scanf(const char *format, ...)
## Parameters
* **format** -- This is the C string that contains one or more of the following items: _Whitespace character, Non-whitespace character_ and _Format specifiers_.
A format specifier follows this prototype:
[=%[*]type=]
The detailed explanation is as follows:
| Parameter | Description |
| --- | --- |
| * | This is an optional asterisk indicating that the data is to be read from the stream but ignored, i.e., it is not stored in the corresponding argument. |
| width | This specifies the maximum number of characters to be read in the current reading operation. |
| modifiers | Specifies a size different from int (for d, i, and n), unsigned int (for o, u, and x), or float (for e, f, and g) for the data pointed to 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) |
| type | A character specifying the type of data to be read and how it is read. See the next table for details. |
**scanf Type Specifiers:**
| Type | Qualifying Input | Argument Type |
| --- | --- | --- |
| %a, %A | Read a floating-point value (C99 only). | float * |
| %c | Single character: Reads the next character. If a width other than 1 is specified, the function reads width characters and passes them via the argument, storing them in consecutive positions of the array. A null character is not appended at the end. | char * |
| %d | Decimal integer: The + or - sign is optional. | int * |
| %e, %E, %f, %F, %g, %G | Floating point: Contains a decimal point, an optional leading + or - sign, an optional trailing e or E, and a decimal digit. Two valid examples are -732.103 and 7.12e4. | float * |
| %i | Reads decimal, octal, or hexadecimal integer. | int * |
| %o | Octal integer. | int * |
| %s | String. This reads consecutive characters until a whitespace character is encountered (whitespace characters can be space, newline, and tab). | char * |
| %u | Unsigned decimal integer. | unsigned int * |
| %x, %X | Hexadecimal integer. | int * |
| %p | Reads a pointer. | |
| %[] | Scans a character set. | |
| %% | Reads the % sign. | |
* **Additional arguments** -- Depending on the format string, the function may require a series of additional arguments, each containing a value to be inserted in place of each % tag specified in the format parameter. The number of arguments should be the same as the number of % tags.
## Return Value
If successful, the function returns the number of items successfully matched and assigned. If the end of file is reached or a read error occurs, it returns EOF.
## Example
The following example demonstrates the usage of the scanf() function.
## Example
#includeint main(void){int a,b,c; printf("Please enter three numbers: "); scanf("%d%d%d",&a,&b,&c); printf("%d,%d,%dn",a,b,c); return 0; }
Let us compile and run the above program, which will produce the following result in interactive mode:
Please enter three numbers: 1 2 31,2,3
Explanation:
* 1. The **&** in **&a, &b, &c** is the address-of operator, which obtains the memory addresses of these three variables respectively.
* 2. **%d%d%d** inputs three values in decimal format. When entering, you can use one or more spaces, tab keys, or enter keys to separate the two data points.
If you use a comma to separate the **%d** inputs, you also need to add a comma in the corresponding input:
## Example
#includeint main(void){int a,b,c; printf("Please enter three numbers: "); scanf("%d, %d, %d",&a,&b,&c); printf("%d, %d, %dn",a,b,c); return 0; }
Let us compile and run the above program, which will produce the following result in interactive mode:
Please enter three numbers: 1, 2, 31, 2, 3
**Note:** When entering, the comma must immediately follow the number, with no space between the number and the comma.
When using %c for input, spaces and "escape characters" are treated as valid characters.
## Example
#includeint main(void){char a,b,c; printf("Please enter three characters: "); scanf("%c%c%c",&a,&b,&c); printf("%c,%c,%cn", a,b,c); return 0; }
Let us compile and run the above program, which will produce the following result in interactive mode:
$ ./a.out Please enter three characters: run r,u,n $ ./a.out Please enter three characters: r u n r, ,u
The following example demonstrates receiving a string:
## Example
#includeint main(){char str1, str2; printf("Please enter your username: "); scanf("%s", str1); printf("Please enter your website: "); scanf("%s", str2); printf("Entered username: %sn", str1); printf("Entered website: %s", str2); return(0); }
Let us compile and run the above program, which will produce the following result in interactive mode:
Please enter your username: admin Please enter your website: www..com Entered username: admin Entered website: www..com
[ C Standard Library - ](#)
YouTip