15_printf详解

printf函数

用法如下图所示:


15_printf详解_第1张图片
printf.png

测试代码

#include 
#include 

#define INTDEMO 250

int main()
{
    const double DOUBLEDEMO = 3.14159;
    const short SHORTDEMO = 0xFFFF;

    printf("%2d\n", INTDEMO);
    printf("%10d\n", INTDEMO);
    printf("%010d\n", INTDEMO);
    printf("%-10d\n", INTDEMO);
    printf("%-010d\n", INTDEMO);

    printf("\n");

    printf("%f\n", DOUBLEDEMO);
    printf("%Lf\n", DOUBLEDEMO);
    printf("%4.2Lf\n", DOUBLEDEMO);
    printf("%10.3Le\n", DOUBLEDEMO);
    printf("%10.3Lf\n", DOUBLEDEMO);
    printf("%010.3Lf\n", DOUBLEDEMO);
    printf("%+010.3Lf\n", DOUBLEDEMO);
    printf("%+010.3Lf\n", -DOUBLEDEMO);

    printf("\n");

    // short类型需要转换成int,所以效率会降低
    printf("%c", SHORTDEMO);

    printf("\n");

    float fNumber = 3.0;
    double dNumber = 3.0;
    int iNumber = 2000000000;
    long lNumber = 1234567890;

    printf("%.lg %.lg %.lg %.lg\n", fNumber, dNumber, iNumber, lNumber);
    printf("%ld %ld\n", iNumber, lNumber);
    printf("%ld %ld %ld %ld\n", fNumber, dNumber, iNumber, lNumber);    // 为什么后面两个参数也打印错了呢?
                                                                        // 原因就是前面前面的占位符决定了取多少字节

    printf("\n");

    printf("Nice""to", "meet you!");

    system("pause");
    return 0;
}

打印结果如下所示:


15_printf详解_第2张图片
1.png

你可能感兴趣的:(15_printf详解)