user336671
user336671

Reputation: 351

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

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;
}

Upvotes: 11

Views: 137105

Answers (8)

Tarka
Tarka

Reputation: 4043

%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 cplusplus — printf for more information.

Upvotes: 22

Polaris000
Polaris000

Reputation: 938

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|

Upvotes: 1

Poornachandra H D
Poornachandra H D

Reputation: 61

Take a look here:

Print("%3d",X);

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

Upvotes: 6

Majid
Majid

Reputation: 14253

An example to enlighten existing answers:

printf("%3d" , x);

When:

x is 1234 prints 1234

x is 123 prints 123

x is 12 prints 12 with an extra padding (space)

x is 1 prints 1 with two extra paddings (spaces)

Upvotes: 1

chandan kumar
chandan kumar

Reputation: 1

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

Upvotes: 0

Mac
Mac

Reputation: 14791

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.

Upvotes: 0

aioobe
aioobe

Reputation: 421090

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

Upvotes: 0

Potatoswatter
Potatoswatter

Reputation: 137870

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.

Upvotes: 12

Related Questions