

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The scanf function in c is used to read formatted data from standard input (stdin). The different components of the scanf function, including format specifiers, parameters, and return values. It also provides examples of how to use scanf to read various data types, such as strings, integers, and hexadecimal numbers.
Typology: Assignments
1 / 3
This page cannot be seen from the preview
Don't miss anything!


int scanf ( const char * format, ... );
Read formatted data from stdin
Reads data from stdin and stores them according to the parameter format into the locations
pointed by the additional arguments. The additional arguments should point to already allocated
objects of the type specified by their corresponding format tag within the format string.
Parameters
format
C string that contains one or more of the following items:
[=%[*][width][modifiers]type=]
where:
*
An optional starting asterisk indicates that the data is to be retrieved from stdin but ignored, i.e. it is not stored in the corresponding argument.
width
Specifies the maximum number of characters to be read in the current reading operation
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)
type
A character specifying the type of data to be read and how it is expected to be read. See next table.
scanf type specifiers:
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 folowed by the e or E character and a decimal number. Two examples of valid entries are -732.103 and 7.12e
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^ *
additional arguments
The function expects a sequence of references as additional arguments, each one pointing to an object of the type specified by their corresponding %-tag within the format string, in the same order. For each format specifier in the format string that retrieves data, an additional argument should be specified. These arguments are expected to be references (pointers): if you want to store the result of a fscanf operation on a regular variable you should precede its identifier with the reference operator , i.e. an ampersand sign (&), like in: int n; scanf ("%d",&n);