C Library Function - scanf()
C Standard Library - <stdio.h>
Description
The C library function int scanf(const char *format, ...) reads formatted input from the standard input 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.
The format specifier is in the form:
[=%[*][width][modifiers]type=]
Detailed explanation is as follows:
Parameter | Description |
---|---|
* | This is an optional asterisk indicating that 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 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 expected to be read. See the next table for details. |
scanf Type Specifiers:
Type | Qualified Input | Argument Type |
---|---|---|
%a, %A | Reads a floating-point value (only valid with C99). | float * |
%c | Single character: reads the next character. If width is specified, reads up to width characters, storing them in an array of contiguous elements, with no null character added at the end. | char * |
%d | Decimal integer: the sign can be optional. | int * |
%e, %E, %f, %F, %g, %G | Floating-point number: includes a decimal point, an optional sign, an optional e or E, and a decimal number. Valid examples are -732.103 and 7.12e4 | float * |
%i | Reads a decimal, octal, or hexadecimal integer. | int * |
%o | Octal integer. | int * |
%s | String of characters. This will read consecutive characters until a whitespace character is encountered. | char * |
%u | Unsigned decimal integer. | unsigned int * |
%x, %X | Hexadecimal integer. | int * |
%p | Reads a pointer. | |
%[] | Scanset of characters. | |
%% | Reads a percent sign. |
- Additional arguments -- Depending on the format string, the function may expect a sequence of additional arguments, each containing a value to be inserted instead of each %-tag specified in the format parameter, with the number of arguments being equal to the number of %-tags.
Return Value
On success, the function returns the number of input items successfully matched and assigned. If the end-of-file is encountered or a read error occurs, EOF is returned.
Example
The following example demonstrates the use of the scanf() function.
#include<stdio.h>
int main(void)
{
int a,b,c;
printf("Please enter three numbers: ");
scanf("%d%d%d",&a,&b,&c);
printf("%d,%d,%d\n",a,b,c);
return 0;
}
Let's compile and run the above program, which will produce the following result in interactive mode:
Please enter three numbers: 1 2 3
1,2,3
Explanation:
- &a, &b, &c The
&
is the address operator, which gets the memory address of these variables.
- &a, &b, &c The
- %d%d%d is the format for reading three decimal values. Input can be separated by one or more spaces, tabs, or newlines.
Example
#include<stdio.h>
int main(void)
{
int a,b,c;
printf("Please enter three numbers: ");
scanf("%d, %d, %d",&a,&b,&c);
printf("%d, %d, %d\n",a,b,c);
return 0;
}
Let's compile and run the above program, which will produce the following result in interactive mode:
Please enter three numbers: 1, 2, 3
1, 2, 3
Note: 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 considered valid characters.
Example
#include<stdio.h>
int main(void)
{
char a,b,c;
printf("Please enter three characters: ");
scanf("%c%c%c",&a,&b,&c);
printf("%c,%c,%c\n", a,b,c);
return 0;
}
Let's 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 accepting a string:
Example
#include <stdio.h>
int main()
{
char str1[20], str2[30];
printf("Please enter your username: ");
scanf("%s", str1);
printf("Please enter your website: ");
scanf("%s", str2);
printf("Entered username: %s\n", str1);
printf("Entered website: %s", str2);
return(0);
}
Let's 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.tutorialpro.org
Entered username: admin
Entered website: www.tutorialpro.org