printf格式控制

ter

Type of Matching Argument

Auto-skip
Leading
White-Space

Example

Sample Matching Input

%

% (a literal, matched but not converted or assigned)

no

int anInt;
scanf("%i%%", &anInt);

23%

d

int (See note)

yes

int anInt; long l;
scanf("%d %ld", &anInt, &l);

-23 200

i

int (See note)

yes

int anInt;
scanf("%i", &anInt);

0x23

o

unsigned int (See note)

yes

unsigned int aUInt;
scanf("%o", &aUInt);

023

u

unsigned int (See note)

yes

unsigned int aUInt;
scanf("%u", &aUInt);

23

x

unsigned int (See note)

yes

unsigned int aUInt;
scanf("%d", &aUInt);

1A

a, e, f, g

float or double (See note)

yes

float f; double d;
scanf("%f %lf", &f, &d);

1.2 3.4

c

char (See note)

no

char ch;
scanf(" %c", &ch);

Q

s

array of char (See note)

yes

char s[30];
scanf("%29s", s);

hello

p

void (See note)

yes

int* pi; void* ptr;
scanf("%p", &ptr);
pi = (int*) ptr;

0064FE00

n

int (See note)

no

int x, cnt;
scanf("X: %d%n", &x, &cnt);

X: 123  (cnt==6)

[

array of char (See note)

no

char s1[64], s2[64];
scanf(" %[^\n]", s1);
scanf("%[^\t] %[^\t]", s1, s2);


Hello World
field1   field2



scanf Format Specification Syntax

The control of input conversion is much simpler than for output conversions.  Any, all, or none of the following format modifiers may be used between the % and the final letter of the conversion specification.  Note these must appear (if at all) in the sequence shown here.  A · is used to indicate a space in the example output where spacing is not obvious.

%   *    maximum-field-width   length   Letter

 

Conversion
Modifier

Description

Example

Matching
Input

Results

*

Assignment Supression.  This modifier causes the corresponding input to be matched and converted, but not assigned (no matching argument is needed).

int anInt;
scanf("%*s %i", &anInt);


Age:·29

anInt==29,
return value==1

maximum
field-width

This is the maximum number of character to read from the input.  Any remaining input is left unread.  (Always use this with "%s" and "%[...]" in all production quality code!  (No exceptions!)  You should use one less than the size of the array used to hold the result.)

int anInt; char s[10];
scanf("%2i", &anInt);

scanf("%9s", s);


2345

VeryLongString

anInt==23,
return value==1

s=="VeryLongS"
return value==1

length
modifier

This specifies the exact type of the matching arugment.  These length codes are the same as theprintf length modifiers, except as noted below:

The same for integer conversions (d, i, o, u, x, or X conversion letters), specifies the matching argument is along int.  For floating point (a, A, e, E, f, F, g, or G conversion letters) an "l" has no effect in a printfbut with scanf, an ell specifies adouble

double d;
scanf("%lf", &d);

3.14

d==3.14
return value==1


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值