在printf中设置可变文本列宽 [英] Set variable text column width in printf

查看:117
本文介绍了在printf中设置可变文本列宽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了确定C语言之列,我们使用的大小%LT;一些D 1和D
举例来说,我可以键入%3D ,它会给我宽度= 3列。
我的问题是,经过我的电话号码的是一个变量,我收到了,所以我需要像%XD (其中, X 是我在某个节目接收之前的整型变量)。
但它不工作。

In order to determine the size of the column in C language we use %<number>d. For instance, I can type %3d and it will give me a column of width=3. My problem is that my number after the % is a variable that I receive, so I need something like %xd (where x is the integer variable I received sometime before in my program). But it's not working.

是否有任何其他方式做到这一点?

Is there any other way to do this?

推荐答案

您可以做到这一点如下:

You can do this as follows:

printf("%*d", width, value);

从李的评论:结果
您也可以使用*为precision尺寸:

From Lee's comment:
You can also use a * for the precision size:

printf("%*.*f", width, precision, value);

这篇关于在printf中设置可变文本列宽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆