printf的使用和int、float、double格式的控制

//printf的使用和int、float、double格式的控制
//%:表示格式说明的起始符号,不可缺少   %i,%f等等就理解成一个占位置的坑,除了这个坑其他位置都随意填写
#include <stdio.h>

int main(int argc, const char * argv[]) {
    //1、输出整型、单精度、双精度、字符类型数据
    int intValue=0;
    float floatValue=1.1;
    double doubleValue=1.1;
    char charValue='c';
    
    printf("%i
",intValue);
    printf("%f
",floatValue);
    printf("%lf
",doubleValue);
    printf("%c
",charValue);
    
    //2、指定位宽输出 %m
    int intValue2=99;
    printf("%5i!!!
",intValue2);//默认情况下右对齐
    printf("%-5i!!!
",intValue2);//左对齐
    // 注意: 如果指定了位宽, 但是实际输出的内容超出了宽度, 会按照实际的宽度来输出
    int intValue3 = 9999;
    printf("%2i
", intValue3);
    int intValue4=9;
    printf("%05i
",intValue4);
    
    //3、保留位数  %.n
    float floatValue2=1.1;
    double doubleValue2=2.22;
    printf("%.3f!!!
",floatValue2);
    printf("%.10lf
",doubleValue2);
    
    //4、指定位宽和保留位数的综合 %m.n
    float floatValue3=12.1234;
    printf("%06.2f
",floatValue3);//位宽是总的宽度
    
    //5、float有效位为7(不包含小数点),double有效为15(不含小数点)
    //默认float都是显示6位小数
    float floatValue4=3.141592653;
    printf("%f
",floatValue4);
    printf("%.10f
",floatValue4);
    
    double doubleValue3=3.141592653545;
    printf("%f
",doubleValue3);
    printf("%.15lf
",doubleValue3);
    
    //6、提高逼格
    printf("%.*f
",4,floatValue4);
    
    return 0;
}
原文地址:https://www.cnblogs.com/imChay/p/5590091.html