• C转义字符


    C语言输出特殊字符

      C语言转义字符意义大体同于前面的C#转义字符,这里列出用c语言,输出%d、 等特殊字符的方法。

    #include <stdio.h>
    
    int main()
    {
        printf("%%d");//在屏幕中输出%d
    
        printf("\n");//在屏幕中输出
    
        
        printf("%d");//0
    
        printf("%%%%%d"); //%%0
    
        printf("%%%%d");//%%d
    
        return 0;
    }

    关于%d、%nd和%0nd

      %d是int类型用于格式化输入输出时对应的格式符号。

      在使用标准输出函数printf进行输出时,有时为了满足某种需要,就要使用%nd和%0nd

      其中:

        %nd 输出的整型宽度至少为n位,右对齐,%5d即宽度至少为5位,位数大于5则输出实际位数

        %0nd 用得比较多,表示输出的整型宽度至少为n位,不足n位用0填充

      如下程序:

    #include<stdio.h>
    
    int main()
    {
        printf("%5d",1);     //输出:****1(*为空格)
        printf("%05d",1);    //输出:00001
        
        return 0;
    }

    关于%f和%lf

      %f和%lf分别是float类型和double类型用于格式化输入输出时对应的格式符号。
      其中:
        %f 对应单精度浮点型float
        %lf 对应双精度浮点型double

      如下程序,计算并输出500/3的值,结果保留3位小数

    #include<stdio.h>
    
    int main()
    {
        printf("%.3lf
    ",500.0/3.0);    //格式符号%.3lf表示保留三位小数
        printf("%.*lf
    ",3,500.0/3.0); //同上语句效果一样,函数调用时格式串中的*号会被参数中的3 代替,这种方式的好处是后面的参数如果采用变量,可以根据前面对该变量的赋值来控制输出格式
        return 0;
    }
  • 相关阅读:
    Scala for the Impatients---(1)Basics
    2.2 Markov Chain
    2.1 Monte Carlo Integration
    1.2 Sampling From Non-standard Distribution
    1.1 Built-in Distributions In Matlab
    Design Pattern -- Builder
    Java Dynamic proxy
    The Difference Between Keypoints and Descriptors
    gcc -l option vs. -L option: The difference
    Stationarity and Independence of Data
  • 原文地址:https://www.cnblogs.com/eniac12/p/4727477.html
Copyright © 2020-2023  润新知