找回密码
 注册
搜索
查看: 1151|回复: 0

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

[复制链接]
发表于 2011-6-10 11:25:28 | 显示全部楼层 |阅读模式
此格式控制符的基本格式为:%[scanfset]



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



测试代码 1 :



#include   <stdio.h>



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表示a到z的所有字符,又如0-9表示0到9这十个数字。所以,当scanfset为0-9时表示只匹配数字串,当scanfset为A-Za-z时表示只匹配字符(包括大小写),当scanfset为^0-9时不匹配所有数字。注意,“-”的字符只有在其左右两边都有有效字符时才有这个作用,否则被认为是普通字符,如“0-4-6-9”匹配的字符为{0,1,2 ,3,,4, -, 6,7,8,9},这样也为输入“-”字符提供了方法。

测试代码 2 :

#include   <stdio.h>

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   <stdio.h>

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   <stdio.h>

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 <stdio.h>

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.0和ubuntu10.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
高级模式
B Color Image Link Quote Code Smilies

本版积分规则

Archiver|手机版|小黑屋|52RD我爱研发网 ( 沪ICP备2022007804号-2 )

GMT+8, 2024-10-9 06:27 , Processed in 0.044960 second(s), 17 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表