如何将参数 n 传递给 printf("%nd", some_int); [英] How to pass parameter n to printf("%nd", some_int);

查看:15
本文介绍了如何将参数 n 传递给 printf("%nd", some_int);的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们都知道在基于 C 的语言中,printf("%11d", some_int); 表示在 11 个字符的字段内右对齐,但是如果我想将这里的常量 11 替换为一个动态变量,我该怎么办?

We all know in C-based languages, printf("%11d", some_int); means right align within an 11 character field, but what if I want to replace this constant 11 here with a dynamic variable, what am I gonna do ?

推荐答案

您可以使用 * 字符在其自己的参数中指定字段宽度:

You can use the * character to specify the field width in its own argument:

printf("%*d", some_width, some_int);

这篇关于如何将参数 n 传递给 printf("%nd", some_int);的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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