相关文章推荐
发怒的卤蛋  ·  ListView.View 属性 ...·  2 年前    · 
文雅的大熊猫  ·  【MobX】MobX ...·  2 年前    · 
腼腆的茶叶  ·  vue3+ts ...·  2 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

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;
                Voted as duplicate to the above - we don't need a seperate "what does %Nd mean?" for every possible N.
– Georg Fritzsche
                May 10, 2010 at 20:32
  • % means "Print a variable here"
  • 3 means "use at least 3 spaces to display, padding as needed"
  • d means "The variable will be an integer"
  • Putting these together, it means "Print an integer, taking minimum 3 spaces"

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

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

    From 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.

  • If X is 1234, it prints 1234.
  • If X is 123, it prints 123.
  • If X is 12, it prints _12 where _ is a leading single whitespace character.
  • If X is 1, it prints __1 where __ is two leading whitespacce characters.
  • 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:

    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|
                    You are answering a quite old question which already has some well received answers. I can't see any added value in your answer. So, what's your reason of posting it?
    – honk
                    Oct 20, 2014 at 17:28
            

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.