Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

printf

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;
}
like image 589
user336671 Avatar asked May 10 '10 20:05

user336671


People also ask

What does %% mean in printf?

As % has special meaning in printf type functions, to print the literal %, you type %% to prevent it from being interpreted as starting a conversion fmt.

What does %- 5D mean in C?

Re “What does printf ("%5D") mean in C?”, it means nothing. It's a syntax error and won't even compile.

What does %- 3d mean in C?

%3d can be broken down as follows: % 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"


1 Answers

%3d can be broken down as follows:

  • % 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

like image 171
Tarka Avatar answered Oct 05 '22 23:10

Tarka