printf()和scanf()

5 篇文章 0 订阅

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.)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值