Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does %*.*d work in printf()?

Tags:

c++

c

printf

#include <stdio.h>

int main()
{
  printf("%*.*d\n", -6 , 7,20000);
  printf("%*.*d\n", 5, -6, 2000);
  return 0;
}

Output:

0020000
 2000

I don't understand how does printf interpret the format specifier * . * ?

While in the first call to printf(),does the later 7 override the former -6? So that the size of output width turns to 7?

like image 379
Andy.G Avatar asked Jul 10 '13 09:07

Andy.G


People also ask

What does %* D mean in C?

The %*d in a printf allows you to use a variable to control the field width, along the lines of: int wid = 4; printf ("%*d\n", wid, 42);

What is %* C in C in print?

printf returns the number of characters printed. printf("%*c", N, C); prints N-1 blanks followed by the character C.

What does %d do in printf?

%d tells printf that the corresponding argument is to be treated as an integer value; the type of the corresponding argument must be int .

What is %d and &N in C?

long double. %n. prints nothing. %d. a decimal integer (assumes base 10)


2 Answers

The argument to the * before the . is the field width and the argument to the * after the . is the precision.

Field widths are the minimum number of bytes that will be output as a result of the conversion; the output will be padded (by default, on the left with spaces, but left zero padding and right space padding are also options, controlled by flags) if fewer bytes would be produced. A negative argument to the * for width is interpreted as the corresponding positive value with the - flag, which moves the padding to the right (i.e. left-justifies the field).

Precision on the other hand has a meaning that varies according to the conversion being performed. Negative precisions are treated as if no precision had been specified at all. For integers, it's the minimum number of digits (not total output) to be produced; if fewer digits would be produced, zeros are added to the left. An explicit precision of 0 results in no digits being produced when the value is 0 (instead of a single 0 being produced). For strings, precision limits the number of output bytes, truncating the string (and permitting a longer, non-null-terminated, input array) if necessary. For floating point specifiers, precision controls the number of places printed, either after the radix point (for %f) or the total places of significance (for the other formats).

In your examples:

printf("%*.*d\n", -6 , 7,20000);

Here the field is left-aligned (padding on right) with a minimum width of 6, but the field ends up being wider anyway, so the width is ignored. The precision of 7 forces integer output to be at least 7 digits, so you end up with 0020000 as the converted field contents, which already exceeded the width.

In the other one:

printf("%*.*d\n", 5, -6, 2000);

The field width is 5, with the default right alignment; padding is with spaces on the left. The negative precision is ignored, as if it were not specified, so the converted field contents are 2000, only 4 bytes, which get padded up to 5 bytes to fill the width by means of a single leading space.

like image 126
R.. GitHub STOP HELPING ICE Avatar answered Sep 17 '22 23:09

R.. GitHub STOP HELPING ICE


printf("%*.*d\n", -6 , 7,20000); 

is same as

printf("%-6.7d\n, 20000);

This just provide a way for dynamic format.

like image 36
TieDad Avatar answered Sep 21 '22 23:09

TieDad