C++ printf 语句中的“%3d”是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2806093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:08:25  来源:igfitidea点击:

What does "%3d" mean in a printf statement?

c++printf

提问by user336671

In this code what is the role of the symbol %3d? I know that % means refer to a variable.

在这段代码中,符号的作用是%3d什么?我知道 % 表示引用一个变量。

This is the code:

这是代码:

#include <stdio.h>
int main(void)
{
    int t, i, num[3][4];
    for(t=0; t<3; ++t)
        for(i=0; i<4; ++i)
            num[t][i] = (t*4)+i+1;
    /* now print them out */
    for(t=0; t<3; ++t) {
        for(i=0; i<4; ++i)
            printf("%3d ", num[t][i]);
        printf("\n");
    }
    return 0;
}

回答by Tarka

%3dcan be broken down as follows:

%3d可以分解如下:

  • %means "Print a variable here"
  • 3means "use at least 3 spaces to display, padding as needed"
  • dmeans "The variable will be an integer"
  • %意思是“在此处打印变量”
  • 3表示“至少使用 3 个空格来显示,根据需要填充”
  • d意思是“变量将是一个整数”

Putting these together, it means "Print an integer, taking minimum 3 spaces"

把这些放在一起,这意味着“打印一个整数,最少占用 3 个空格”

See http://www.cplusplus.com/reference/clibrary/cstdio/printf/for more information

有关更多信息,请参阅http://www.cplusplus.com/reference/clibrary/cstdio/printf/

回答by Potatoswatter

That is a format specifier to print a decimal number (d) in three (at least) digits (3).

这是一个格式说明符,用于d以三个(至少)数字 ( 3)打印十进制数( )。

From man printf:

来自man printf

An optional decimal digit string specifying a minimum field width. If the converted value has fewer characters than the field width, it will be padded with spaces on the left (or right, if the left-adjustment flag has been given) to fill out the field width.

指定最小字段宽度的可选十进制数字字符串。如果转换后的值的字符数少于字段宽度,则会在左侧(或右侧,如果已给出左调整标志)填充空格以填充字段宽度。

回答by Poornachandra H D

Take a look here:

看看这里:

Print("%3d",X);

Print("%3d",X);

  • If X is 1234, it prints 1234.
  • If X is 123, it prints 123.
  • If X is 12, it prints _12where _is a leading single whitespace character.
  • If X is 1, it prints __1where __is two leading whitespacce characters.
  • 如果 X 为 1234,则打印1234
  • 如果 X 为 123,则打印123
  • 如果 X 为 12,则打印_12where_是前导的单个空白字符。
  • 如果 X 为 1,则打印__1where__是两个前导空格字符。

回答by Polaris000

You can specify the field width between the % and d(for decimal). It represents the total number of characters printed. A positive value, as mentioned in another answer, right-aligns the output and is the default. A negative value left-aligns the text. example:

您可以指定 % 和 d(十进制)之间的字段宽度。它表示打印的字符总数。如另一个答案中所述,正值将输出右对齐并且是默认值。负值左对齐文本。例子:

int a = 3;
printf("|%-3d|", a);

The output:

输出:

|3  |

You could also specify the field width as an additional parameter by using the * character:

您还可以使用 * 字符将字段宽度指定为附加参数:

int a = 3;
printf("|%*d|", 5, a);

which gives:

这使:

|    3|

回答by Majid

An example to enlighten existing answers:

启发现有答案的示例:

printf("%3d" , x);

When:

什么时候:

xis 1234 prints 1234

x是 1234 张 1234

xis 123 prints 123

x是 123 张 123

xis 12 prints 12with an extra padding (space)

x12带有额外填充(空格)的12 个打印件

xis 1 prints 1with two extra paddings (spaces)

x是 1 个1带有两个额外填充(空格)的打印

回答by aioobe

It is a formatting specification. %3d says: print the argument as a decimal, of width 3 digits.

它是一种格式规范。%3d 说:将参数打印为宽度为 3 位的十进制数。

回答by Mac

Literally, it means to print an integer padded to three digits with spaces. The % introduces a format specifier, the 3 indicates 3 digits, and the d indicates an integer. Thus, the value of num[t][i] is printed to the screen as a value such as " 1", " 2", " 12", etc.

从字面上看,这意味着打印一个用空格填充为三位数的整数。% 引入了格式说明符,3 表示 3 位数字,d 表示整数。因此,num[t][i] 的值会作为“1”、“2”、“12”等值打印到屏幕上。

回答by chandan kumar

2/3 or any integer is padding/width .it means ex for 3 ,minimum 3 space if we print a=4 then it print like 4,here two space left before 4 because it is one character

2/3 或任何整数是 padding/width 。这意味着 ex for 3 ,如果我们打印 a=4 则最少 3 个空格然后它打印为 4,这里在 4 之前留下两个空格,因为它是一个字符