zoukankan      html  css  js  c++  java
  • 读书共享 Primer Plus C-part 4

      第四章 字符串和格式化输入输出

    • 关于printf

    -  左对齐

     

    #include<stdio.h>
    
    void main()
    {
      char str [100] = "liuchuanwu";
      printf("%20s
    ",str);
      printf("%-20s
    ",str);
    
    
    
    }
    

      

    • 关于浮点数的打印

           

    #include<stdio.h>
    #include<string.h>
    int main()
    {
      char str [100] = "liuchuanwu";
      short s1 = 1111;
      short s2 =1;
      int   i1= 1111111111;
      long int l1= 1111111111111111111;
      long int l2=11;
      float f1 = 1234.6789;
      printf("%d  %d
    ",sizeof(s1), s1);
      printf("%d  %d
    ",sizeof(s2), s2);
      printf("%d  %d
    ",sizeof(i1), i1);
      printf("%d  %ld
    ",sizeof(l1), l1);
      printf("%d  %ld
    ",sizeof(l2), l2);
      printf("%20s
    ",str);
      printf("%-20s
    ",str);
      printf("%d 
      %d
    ",sizeof(str),strlen(str));
    
    
      printf("%2.3f 
    ",f1);
      printf("%0.3f 
    ",f1);
      printf("%-10.3f 
    ",f1);
      printf("%10.3f 
    ",f1);
      return 0;
    
    
    }

        %x.y x小于浮点数本身的大小全部打印 x大于浮点数本身按照x长度打印。

         x的长度 指整数长度+小数长度+1

           

    •  关于字符串打印长度

        

    #include<stdio.h>
    #include<string.h>
    int main()
    {
      char str [100] = "liuchuanwu";
      printf("%20.1s 
    ",str);
      return 0;
    
    
    }

      对于%20.1s 20指的是整个打印占多大,.1指的是打印多少个真正的字符。

           

    • 对于长字符串处理

         

    #include<stdio.h>
    #include<string.h>
    int main()
    {
      char str [100] = "liuchuanwu";
      printf("%20.1s 
    ",str);
      printf("liuchuanwu is a handman
    ");
      printf("liuchuanwu is a 
    handman
    ");
      printf("liuchuanwu is a "
              "handman
    ");
    
    
      return 0;
    
    
    }

          

    •  关于sizeof 和strlen

         

    #include<stdio.h>
    #include<string.h>
    int main()
    {
      char str [100] = "liuchuanwu";
      printf("%20s
    ",str);
      printf("%-20s
    ",str);
      printf("%d 
      %d
    ",sizeof(str),strlen(str));
      return 0;
    
    
    }
    

      sizeof 所占字节大小 strlen到还有多久

          

    •  关于scanf

             scanf 的第二个输入参数是指针,所以针对基本类型需要使用&获取内存地址,对于字符串则不需要,字符串本身为指针。

           

    #include<stdio.h>
    #include<string.h>
    int main()
    {
      char str [100] = "liuchuanwu";
      int age = 0;
      printf("input your name and age 
    ",str);
      scanf("%s",str);
      scanf("%d",&age);
      printf("%d 
    %s
    ",age,str);
    
      return 0;
    
    
    }

            

         残留问题针对空行如何处理

         

  • 相关阅读:
    Opencv 图片边缘检测和最小外接矩形
    python matplotlib包图像配色方案
    python matplotlib 绘图 和 dpi对应关系
    python 工具 二进制文件处理之——去掉指定长度数据包头
    python 工具 二进制文件处理之——大小端变换
    Prime Path(POJ 3126 BFS)
    Travel(HDU 5441 2015长春区域赛 带权并查集)
    树上战争(HDU 2545 并查集求解点到根节点长度)
    More is better(hdu 1856 计算并查集集合中元素个数最多的集合)
    How Many Tables(POJ 1213 求连通分量)
  • 原文地址:https://www.cnblogs.com/liuchuanwu/p/7092929.html
Copyright © 2011-2022 走看看