zoukankan      html  css  js  c++  java
  • 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 ", 100, &n);

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

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

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

  • 相关阅读:
    【初学EXT】布局练习
    创建型模式总结(补充UML类图)
    数据库基本概念总结
    Word2010操作技巧总结
    VMWare虚拟机磁盘压缩和上网总结
    第一次用word2010发布文章到博客园记
    设计模式学习总结一原则及创建型模式
    为什么要开始写blog?
    Delphi异常处理总结
    设计模式总结之行为型模式
  • 原文地址:https://www.cnblogs.com/develon/p/9156775.html
Copyright © 2011-2022 走看看