电脑桌面
添加小米粒文库到电脑桌面
安装后可以在桌面快捷访问

C语言printf与scanf讲解

C语言printf与scanf讲解_第1页
1/18
C语言printf与scanf讲解_第2页
2/18
C语言printf与scanf讲解_第3页
3/18
scanf 格式控制字符串% [Reading Undelimited strings] *To read strings not delimited by whitespace characters, a set of characters in brackets ([ ]) can be substituted for the s (string) type character. The set of characters in brackets is referred to as a control string. The corresponding input field is read up to the first character that does not appear in the control string. If the first character in the set is a caret (^), the effect is reversed: The input field is read up to the first character that does appear in the rest of the character set. *Note that %[a-z] and %[z-a] are interpreted as equivalent to %[abcde...z]. This is a common scanf function extension, but note that the ANSI standard does not require it. 举一些例子: 对于 const char* p = "12232114687ABC12356"; sscanf(p,"%[123]",buf); // 就把是'1'或'2'或'3'的字读读到buf 中,直到遇到一个不是'1'且不是'2'且不是'3'的字符,于是执行后buf 应该是"1223211"; %[123]等同于%[231],等同于%[321]„„,列表中的顺序是无所谓的; %[123]也等同于%[1-3]或%[3-1],也就是“1 至 3”,对于连续的字符列表这样写就很简单,比如%[a-z]等同%[abc„省略„z]; 想想看,%[3-14]应该等同于什么?是“3 至 14”吗?当然不是,因为[]中的是字符,而不是数字,所以%[3-14]应该等同于%[3214],等同于%[1234]; 同理,想只取字母,那就可以写成%[A-Za-z]; 如果列表的第一个字母是^,那么正好相反,比如%[^A-Za-z]的意思就是取字母之外的所有字符。 对于字符串"abDEc123"如果想按照字母和数字读到两个字符串中就应该是 "%[a-zA-Z]%[0-9]",buf1,buf2 ; 假如我想取一行字符,该怎么办?"%s"是不行的,因为%s 遇到空白 字 符( 空 格 、 制 表符、 \r 、 \n ) 就结 束 了 ,所 以 可 以 写成 "%[^\n]%*c",%[^\n]的作用刚才讲过了,就是读\n 之外的所有字符,也就是说读到\n 为止,%*c 的作用就是把\n 去掉,否则再次读的时候一直遇到的都是\n; 所有对%s 起作用的控制,都可以用于%[],比如"%*[^\n]%*c"就表示跳过一行,"%-20[^\n]"就表示读取\...

1、当您付费下载文档后,您只拥有了使用权限,并不意味着购买了版权,文档只能用于自身使用,不得用于其他商业用途(如 [转卖]进行直接盈利或[编辑后售卖]进行间接盈利)。
2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。
3、如文档内容存在违规,或者侵犯商业秘密、侵犯著作权等,请点击“违规举报”。

碎片内容

C语言printf与scanf讲解

确认删除?
VIP
微信客服
  • 扫码咨询
会员Q群
  • 会员专属群点击这里加入QQ群
客服邮箱
回到顶部