15_printf详解

2018-01-02  本文已影响0人  yishurensheng

printf函数

用法如下图所示:


printf.png

测试代码

#include <stdio.h>
#include <stdlib.h>

#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;
}

打印结果如下所示:


1.png
上一篇下一篇

猜你喜欢

热点阅读