// width.cpp : 定义控制台应用程序的入口点。
//
/* width.c -- 字段宽度 */
/*
时间:2018年06月15日 23:43:01
代码:程序清单4.7_width.c程序_《C Primer Plus》P71
目的:了解 printf() %d d前加参数的输出结果
*/
#include "stdafx.h"
#define PAGES 931
int _tmain(int argc, _TCHAR* argv[])
{
printf("*%d*\n", PAGES);
printf("*%2d*\n", PAGES);
printf("*%10d*\n", PAGES);
printf("*%-10d*\n", PAGES);
getchar();
return 0;
}
/*
在VS2010中运行结果:
--------------------------------------------------------
*931*
*931*
* 931*
*931 *
--------------------------------------------------------
总结:
1>.%2d 当参数大于2时,则显示参数的真实位数;
2>.%10d 显示10位靠右的数字,左则无数则以空格符替代;
3>.%-10d 显示10位靠左的数字,右则无数则以空格符替代;
--------------------------------------------------------
*/转载于:https://blog.51cto.com/13555061/2129987
本文通过一个C语言示例程序展示了如何使用printf函数中的字段宽度设置来控制输出的格式。具体介绍了%d前加参数的不同效果,包括数字的对齐方式和空格填充等。

被折叠的 条评论
为什么被折叠?



