Function scanf

Top 

Declaration:

int scanf(char format[], ... );

Description

The scanf function displays prompt to enter a character string. The string you enter is parsed in accordance with the format line.

scanf scans a series of input fields one character at a time reading from a stream. After that, each field is formatted in accordance with a format specifier passed to scanf in the format string pointed to by format. Finally, scanf stores the formatted input at the address passed to it as the argument following the format. The number of format specifiers and addresses must be the same as the number of input fields.

Notes

1.

   Your arguments passed to this function shall match the format line. In case of mismatch, the ChipProg program may crash, because it cannot check the correspondence between the format string and parameters passed. For details on format specifiers, see the scanf Format Specifiers.

2.

All arguments for this function shall be arrays, because only the array parameters are passed by address to functions. Also, see example below.

scanf can stop scanning a particular field before it reaches the normal end-of-field character (whitespace) or it can terminate entirely for a number of reasons. See scanf for a discussion on possible causes.

Returned Value

scanf returns the number of input fields successfully scanned, converted and stored. The return value does not include the scanned fields that were not stored. If no fields are stored, then 0 will be returned.

Example

int i[1];

float f[1];

char name[64];

scanf("%d %f %s", i, f, name);

// If "123  4.56  String" is entered in the prompt, then:

// i[0] will assume value 123,

 

// name will be equal to the string "String".