关于C语言基本格式化输入、输出 (如%20[^#] )这种不常见的写法说明
关于C语言基本格式化输入、输出 (如%20[^#] )这种不常见的写法说明
燃烧NO1 发表于4年前
关于C语言基本格式化输入、输出 (如%20[^#] )这种不常见的写法说明
  • 发表于 4年前
  • 阅读 53
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 十分钟定制你的第一个小程序>>>   

摘要: 关于C语言基本格式化输入、输出 (如%20[^#] )这种不常见的写法说明

【关于C语言基本格式化输入、输出 (如%20[^#] )这种不常见的写法说明】
针对C语言中的基本格式化输入、输出,我们常见是格式为:
  %[标志][宽度][.精度]类型
相关说明:
  类型:说明数据类型是整型(d)还是字符串(s)、字符(c)、地址输出(p)等.想输出% 需要写成"%%"。
  标志:“-”左对齐;“+”右对齐(默认是这种方式);空格“ ” 只有当是数值型的输出才有用( 当正数时,则显示一个空格,如是负数 ,则显示一个负号);# 表示哪种进制数。
  宽度:表示最少占用的字符数。当实际的位数大于指定宽度,按实际的输出,否则需要进行填充操作,补空格或是0 。如printf("%04s","44");----结果为0044。
  精度:精确控制个数。如printf("%.2d",3.143);--- 结果3.14  ;printf("%.2s","qwert"); ----结果qw。
  注意:字符串形式只能补空格printf("[%12.12s]","123456"),数值型只能补0 printf("[%012d]" ,1235435);
------------------------------------------------------------------------------------------------------------------------
针对不常见进行举例:
[mengfh /home/mengfh/worker/test]>cat testPrint.c
#include<stdio.h>
#include<string.h>

int main(int argc , char *args[])
{

char buff [255];

memset(buff,0 ,sizeof(buff));
fscanf(stdin, "%[^#]" , buff);
fprintf(stderr, "[%s]\n",buff);

return 0;
}

=====
编译运行结果为:
[mengfh /home/mengfh/worker/test]>gcc -o testPrint testPrint.c
[mengfh /home/mengfh/worker/test]>./testPrint
kjdfkdjkfj#fdjkfjdk
[kjdfkdjkfj]

说明---------其在#进了截断操作并返回。
--------------------------------------------------------------------------------------

“[”

Matches a nonempty sequence of characters from the specified set of accepted characters; the next pointer must be a pointer to char, and there must be enough room for all the characters in  the string, plus a terminating NUL character. The usual skip of leading white space is suppressed. The string is to be made up of characters in (or not in) a particular set; the set is defined by the characters between the open bracket [ character and a close bracket ]character. The set excludes those charac-ters if the first character after the open bracket is a circum-flex ^. To include a close bracket in the set, make it the first character after the open bracket or the circumflex; any other position will end the set. The hyphen character - is also special; when placed between two other characters, it adds all intervening characters to the set. To include a hyphen, make it the last character before the final close bracket. For instance, `[^]0-9-]' means the set `everything except close bracket, zero through nine, and hyphen'. The string ends with the appearance of a character not in the (or, with a circumflex, in) set or when the field width runs out.

共有 人打赏支持
粉丝 24
博文 87
码字总数 67020
×
燃烧NO1
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: