Categories

วันจันทร์ที่ 13 กรกฎาคม พ.ศ. 2552

C scanf(), scanf_s()

scanf() and scanf_s() - อ่านข้อมูลจาก standard input char a, b[20]; int i; scanf("%c %s %d", &a, b, &i); scanf_s("%c %s %d", &a, b, 20, i); /* scanf_s ทำมากัน buffer overflow เวลารับ string ต้องใสขนาดด้วย*/

c

Character. When used with scanf() functions, specifies single-byte character; when used with wscanf() functions, specifies wide character. White-space characters that are ordinarily skipped are read when c is specified. To read next non–white-space single-byte character, use %1s; to read next non–white-space wide character, use %1ws.

Pointer to char when used with scanf() functions, pointer to wchar_t when used with wscanf() functions.

Required. Size does not include space for a null terminator.

C

Opposite size character. When used with scanf() functions, specifies wide character; when used with wscanf functions, specifies single-byte character. White-space characters that are ordinarily skipped are read when C is specified. To read next non–white-space single-byte character, use %1s; to read next non–white-space wide character, use %1ws.

Pointer to wchar_t when used with scanf functions, pointer to char when used with wscanf() functions.

Required. Size argument does not include space for a null terminator.

d

Decimal integer.

Pointer to int.

No.

i

Decimal, hexadecimal, or octal integer.

Pointer to int.

No.

o

Octal integer.

Pointer to int.

No.

u

Unsigned decimal integer.

Pointer to unsigned int.

No.

x

Hexadecimal integer.

Pointer to int.

No.

e, E, f, g, G

Floating-point value consisting of optional sign (+ or –), series of one or more decimal digits containing decimal point, and optional exponent ("e" or "E") followed by an optionally signed integer value.

Pointer to float.

No.

n

No input read from stream or buffer.

Pointer to int, into which is stored number of characters successfully read from stream or buffer up to that point in current call to scanf functions or wscanf() functions.

No.

s

String, up to first white-space character (space, tab or newline). To read strings not delimited by space characters, use set of square brackets ([ ]), as discussed in scanf() Width Specification.

When used with scanf() functions, signifies single-byte character array; when used with wscanf() functions, signifies wide-character array. In either case, character array must be large enough for input field plus terminating null character, which is automatically appended.

Required. Size includes space for a null terminator.

S

Opposite-size character string, up to first white-space character (space, tab or newline). To read strings not delimited by space characters, use set of square brackets ([ ]), as discussed in scanf() Width Specification.

When used with scanf() functions, signifies wide-character array; when used with wscanf functions, signifies single-byte–character array. In either case, character array must be large enough for input field plus terminating null character, which is automatically appended.

Required. Size includes space for a null terminator.

ไม่มีความคิดเห็น:

แสดงความคิดเห็น

Search