c语言void类型的格式化打印,关于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

#include

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.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值