C Standard Libraries C++

scanf()

Declaration

int scanf(const char* kcpFormatString, ...);

Description

This function reads formatted data of the format given by the formatted string "kcpFormatString" from the standard input stream "stdin." The function returns the fields successfully read and assigned.

Format Specifiers:

The format specification takes the following form:

%<*><width><size><type>

The '%' begins character signals the potential input into an argument, and the remaining optional fields specify the format and type of the argument (see the tables below). The asterisk field specifies that the field will be not be assigned to the next argument, but it will be read tand the argument will be skipped.



WidthEffect

integerSpecifies the maximum number of bytes read for the argument. Fewer bytes may be read if a whitespace character is encountered.



SizeEffect

hArgument types d, i, o, x, and X are specified as short. Argument type u is specified as unsigned short. Argument types c and s are specified as single byte ASCII.
lArgument types d, i, o, x, and X are specified as long. Argument type u is specified as unsigned long. Argument types e, E, f, g, and G are specified as double. Argument types c and s are specified as multi-byte UNICODE.
llArgument types d, i, o, x, and X are specified as long long.
LArgument types e, E, f, g, and G are specified as long double.



TypeEffect

cRead "chars" from single-byte ASCII format.
dRead "ints" from signed decimal integers.
e, E, f, g, GRead "floats" in with optional 'e' or 'E' and number for the exponent.
iRead "ints" from signed decimal integers, hexadecimal with '0x' or '0X' prefix or octal with a '0' prefix.
nRead the number of characters read in so far into an int pointer.
oRead "ints" from octal integers.
sRead a null-terminated ASCII character string. Read up to the first whitespace character.
uRead "ints" from unsigned decimal integers.
xRead "ints" from unsigned hexadecimal integers.

Example

#include <cstdio>

int main()
{
    int iInteger = 0;
    // Read a value into the integer
    int iRet = scanf("%i", &iInteger);

    printf("You typed: %i\n", iInteger);

    return 0;
}

Output

scanf() Output
 

© 2007–2024 XoaX.net LLC. All rights reserved.