scanf()函数中%[]格式控制符用法


scanf()函数中%[]格式控制符用法


作者:武汉华嵌-嵌入式培训中心     助理讲师     张老师

 

此格式控制符的基本格式为:%[scanfset]

 

scanfset 有两种形式:一种是以非 “^” 字符开头的 scanset , 表示在读入字符串时将匹配所有在 scanfset 中出现的字符,遇到非scanfset 中的字符时输入就结束;另外一种形式是以 “^” 字符开头的scanfset ,表示在读入字符串时将匹配所有不在scanfset 中出现的字符,遇到scanfset 中的字符输入就结束。

 

测试代码 1 

 

#include  

 

int main()

{

              char str[40] = { 0 } ;

      

              scanf( “%[^\n]” , str ) ; //遇到回车键时字符串输入结束

              printf( “%s\n“ , str ) ;

 

              return 0 ;

}

 

输入 hhhh   &&&ni

Vc6.0的输出结果: 

Ubuntu10.04的输出结果:

       hhhh   &&&ni

 

 

“-”字符的使用。当“-”出现在scanfset中且两边都有字符时,大多数编译器都做了如下所述实现:“-”表示匹配从其左边的字符到右边字符之间所有的字符(按ASCII码排序)。如a-z表示az的所有字符,又如0-9表示09这十个数字。所以,当scanfset0-9时表示只匹配数字串,当scanfsetA-Za-z时表示只匹配字符(包括大小写),当scanfset^0-9时不匹配所有数字。注意,“-”的字符只有在其左右两边都有有效字符时才有这个作用,否则被认为是普通字符,如“ 0-4-6-9”匹配的字符为{012 3,4 - 6,7,8,9},这样也为输入“-”字符提供了方法。

测试代码 2 

#include  

int main()

{

char str[40] = { 0 } ;

   

           scanf( “%[a-c-2-7-A-Z]” , str ) ;//遇到不是方括号中的字符输入就结束,注意字符 “-”

           printf( “%s\n“ , str ) ;

             

           return 0 ;

}

       输入:b35U-

Vc6.0输出结果: 

Ubuntu10.04输出结果:
       b35U-

      

注意:

当要匹配右侧 “]” 或者 “^” 时,得这样去做。如果是右侧方括号时,得把它放在紧跟在左侧方括号的后边,也不可以有空格,如:%[]ajdfidfj ] ;如果是 “^” 时,得不可以把它置于紧跟在左侧方括号的后边,如:%[ gfadhfu^fhgiu ] 

      

测试代码 3 

#include  

int main()

{

char str[40] = { 0 } ;

   

           scanf( “%[ni^hao^ma]” , str ) ;//遇到不是方括号中的字符输入就结束,注意字符 “^”

           printf( “%s\n“ , str ) ;

             

           return 0 ;

}

输入:hao^
Vc6.0输出结果:

Ubuntu10.04输出结果:

 

 hao^
 

测试代码 4 

#include  

int main()

{

           char str[40] = { 0 } ;

             

           scanf(“%[]ni^hao^ma]”,str) ;//遇到不是方括号中的字符输入就结束,注意字符 “]”                 printf( “%s\n“,str) ;

 

           return 0 ;

}

        

输入:hao]^

Vc6.0输出结果:

 

Ubuntu10.04输出结果:

       hao]^

 

测试代码 5 

#include

void put()

{

       char str1[40] = {0};

 

       scanf("%[]nihao]",str1);

 

       printf("%s\n\n",str1);

}

 

int main()

{

       char str1[40] = {0};

       char str2[40] = {0};

 

       scanf("%[ni^hao^ma]",str1);

       printf("%s\n\n",str1);

             

//            fflush(stdin);     刷新键盘缓冲区的作用,但在ubunt10.04上面不起作用

//            getchar();        vc6.0ubuntu10.04上都起作用,接收一个字符,即为键盘缓冲区的 \r ,也就是回车。

 

   scanf("%[]nihao]",str2);

printf("%s\n\n",str2);

 

//            fflush(stdin);

//            getchar();

 

       put();

 

       return 0;

}

 

输入:nihao^ 然后按回车键,整个程序就结束了。

输出:

Vc6.0的输出结果                                                  

Ubuntu10.04输出的结果:

 

 nihao^
        nihao^

 

vc6.0中,当去掉fflush()或者 getchar()的注释时:

结果:
       nihao^
       nihao^

 

 

ubuntu10.04中,当去掉getchar()的注释时:

结果:

hao^
       hao^

 

hao]
       hao]

 

hao^
       hao


(原文出处:武汉华嵌 http://www.embedhq.org/, 转载请注明出处)

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值