C JAVA你可能不知道的那些编程细节

关于printf的格式化字符

%*

  *与其它占位符结合使用, *将首先被一个 int 变量值代替后再被格式化.

  如

  printf("%.*s.", 2, "Hello") 将只输出 "He."

  printf("%-*s", 8, "Hello") 将输出 "Hello   ."

  printf("%*s", 8, "Hello") 将输出"   Hello."

  %*还有一个重要作用, 那就是在scanf中忽略元素, 看下面一个实例

 

develon@Topmain:~/c/mosh$ mosh-server

MOSH CONNECT 60005 BHsEm4OEwbLvtHlkLuBpsg

mosh-server (mosh 1.2.5) [build mosh 1.2.5]
Copyright 2012 Keith Winstein <mosh-devel@mit.edu>
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

[mosh-server detached, pid = 25923]

  要想获取下面两个元素, 首先使用gets(char *), 其次使用sscanf("%*s %*s %d %s", &port, &passwd)即可           ! gets不安全 建议使用fgets(char *, int, FILE *);

60005 BHsEm4OEwbLvtHlkLuBpsg
  

%n

  将 printf 函数已经格式化完毕的字节数量存入 int * 变量指向的 int 变量中

  如

  int n = 0;

  printf("成绩%d分%n\n", 100, &n);

  n 的值将被 printf 函数更改为 UTF-8 编码的字符串 "成绩100分" 所占的字节数 15 .

   由于安全原因, 在 ASOP 项目 bionic 中不支持该特性.

  Java 中, %n 代表平台无关的换行符(主要为了兼容 Windows 平台的换行符 "\n\r" ).

 

posted @ 2018-06-08 17:28  develon  阅读(105)  评论(0编辑  收藏  举报