GNU关于printf()
函数的说明。原来C的printf()
也支持参数索引。scanf()
还支持字符过滤。
#include <stdio.h>
int main(void){
int a=9898;
printf("%1$d, %1$d, %1$d\n", a);
char s[100];
int ret = scanf("%99[0-9]",s);
if (ret > 0){
printf("input is: %s\n", s);
}else{
printf("Invalid input.\n");
}
return 0;
}
printf的格式字符串语法
% [ param-no $] flags width [ . precision ] type conversion
or
% [ param-no $] flags width . * [ param-no $] type conversion
‘%25[1234567890]’
Matches a string of up to 25 digits.
‘%25[][]’
Matches a string of up to 25 square brackets.
‘%25[^ \f\n\r\t\v]’
Matches a string up to 25 characters long that doesn’t contain any
of the standard whitespace characters. This is slightly different
from ‘%s’, because if the input begins with a whitespace character,
‘%[’ reports a matching failure while ‘%s’ simply discards the initial whitespace.
‘%25[a-z]’
Matches up to 25 lowercase characters.
"%a[a-zA-Z0-9] = %a[^\n]\n"
An optional flag character ‘a’ (valid with string conversions only) which requests allocation of a buffer long enough to store the string in. (This is a GNU extension.)