C++ Programming/Code/Standard C Library/Functions/scanf

scanf
The scanf function reads input from stdin, according to the given format, and stores the data in the other arguments. It works a lot like printf. The format string consists of control characters, whitespace characters, and non-whitespace characters. The control characters are preceded by a % sign, and are as follows:

scanf reads the input, matching the characters from format. When a control character is read, it puts the value in the next variable. Whitespace (tabs, spaces, etc.) are skipped. Non-whitespace characters are matched to the input, then discarded. If a number comes between the % sign and the control character, then only that many characters will be converted into the variable. If scanf encounters a set of characters, denoted by the %[] control character, then any characters found within the brackets are read into the variable. The return value of scanf is the number of variables that were successfully assigned values, or EOF if there is an error.

This code snippet uses scanf to read an int, float, and a double from the user. Note that the variable arguments to scanf are passed in by address, as denoted by the ampersand (&) preceding each variable:


 * Related topics: fgets - fscanf - printf - sscanf