代码:程序清单4.16_varwid.c程序_《C Primer Plus》P81

// varsid.cpp : 定义控制台应用程序的入口点。
//
/* varwid.c -- 使用可变宽度输出字段 */

/*
    时间:2018年06月21日 00:10:08
    代码:程序清单4.16_varwid.c程序_《C Primer Plus》P81
    目的:使用 scanf() 输入数字来改变输出字段的宽度
*/

#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
    unsigned width, precision;
    int number = 256;
    double weight = 242.5;

    printf("What field width?\n");
    scanf("%d", &width);
    printf("The number is: %*d: \n", width, number);
    printf("Now enter a width adn a precision: \n");
    scanf("%d %d", &width, &precision);
    printf("Weight = %*.*f\n", width, precision, weight);
    getchar();
    getchar();

    return 0;
}

/*
    在VS2010中运行结果:
------------------------------------------
What field width?
6
The number is:    256:
Now enter a width adn a precision:
8 3
Weight =  242.500
------------------------------------------
    google 翻译如下:

什么字段宽度?
6
该数字是:256:
现在输入宽度和精度:
8 3
重量= 242.500
------------------------------------------
    总结:
        利用 scanf() 交互来改变输出字段宽度
------------------------------------------
*/


猜你喜欢

转载自blog.51cto.com/13555061/2131145
今日推荐