-3

I saw so many question and answer on stackoverflow and other sites but I am so confused that when %2d created 2 character spaces and we want to execute this code then what should happen?

int a = 20011;
printf("%2d\n",a);

According to my point of view it should print only 2 character wide but when we print then it print whole character. Why?

I have edited this question. Now I would like to know that what is used of - sign. I have read so many articles that says - sign tells the left alignment but when it is used give with example. for example:

int str[] = "skgskg";
 printf("%2s\n",str);
Sumit Kumar Gupta
  • 2,132
  • 1
  • 22
  • 21
  • 5
    It only affects string representations that are less than 2 characters wide. If more are needed, it will still output all. – Jeff Mercado Apr 08 '19 at 18:09
  • 2
    `printf` and friends only support a max field width for strings. There is no way to limit how many characters are printed for an integral value. The width specification is the *minimum* width, not the *exact* width. – owacoder Apr 08 '19 at 18:11
  • 4
    Points of view don’t matter much. What matters is what the standard says: `printf` is a C standard function. That’s where the meaning of the format string is defined. And the meaning is such that `2` in your case is the minimum width: if `a` is zero, there would be padding added, since `0` is only one character wide :). – Kuba hasn't forgotten Monica Apr 08 '19 at 18:12
  • Suppose %2s instead of %2d and string like this char str[] = "geeksforgeeks"; what will be output – Sumit Kumar Gupta Apr 08 '19 at 18:13
  • Could you tell me when we write %-2s then what will affect. When i see the output then I see that no any change in output – Sumit Kumar Gupta Apr 08 '19 at 18:15
  • "geeksforgeeks". To limit the string length, you need to use the precision field: `printf("%.2s", "geeksforgeeks")` prints "ge". – owacoder Apr 08 '19 at 18:16
  • Read [the relevant standard paragraph](http://port70.net/~nsz/c/c11/n1570.html#7.21.6.1p4) and ask if something is not clear. – Eugene Sh. Apr 08 '19 at 18:18
  • @EugeneSh. Please explain the meaning of - for example %-2s when it will use – Sumit Kumar Gupta Apr 08 '19 at 18:19
  • 1
    You can't have read that helpful link in less than 1 minute, which clearly answers that. – Weather Vane Apr 08 '19 at 18:20
  • http://port70.net/~nsz/c/c11/n1570.html#7.21.6.1p6 - *The flag characters and their meanings are: **'-'** The result of the conversion is left-justified within the field. (It is right-justified if this flag is not specified.)* And then comes the minimum width specifier – Eugene Sh. Apr 08 '19 at 18:22
  • "*what will be output*" - Why are you asking thousands of people online? You could either just try it or read the documentation for `printf` (and if anything is unclear, ask a specific question about that). – melpomene Apr 08 '19 at 18:23
  • @melpomene. Hi I have read so many articles about it but I am so confused because when we execute the code then we find no change in output for example: %2s and %-2s then no any changes in output – Sumit Kumar Gupta Apr 08 '19 at 18:25
  • I suggest you try it with a number smaller than the field width. For example if the field width is `%2d` try it with a number in the range 0..9. Or with your value, a greater field width, such as `int a = 20011; printf("%10d\n",a);` – Weather Vane Apr 08 '19 at 18:26
  • 2
    Possible duplicate of [what is the %2d in scanf](https://stackoverflow.com/questions/13913848/what-is-the-2d-in-scanf) – chickity china chinese chicken Apr 08 '19 at 18:29
  • Note, your question is not mentioning the `-` flag which you are insisting on asking about in the comments. – Eugene Sh. Apr 08 '19 at 18:29
  • 1
    @davedwards No, that question is about `scanf`, not `printf`. – melpomene Apr 08 '19 at 18:33
  • @melpomene correct, because that is what [What is the use and meaning of %2d in c? \[duplicate\]](https://stackoverflow.com/questions/52448726/what-is-the-use-and-meaning-of-2d-in-c?noredirect=1&lq=1) is a duplicate of – chickity china chinese chicken Apr 08 '19 at 18:34
  • @davedwards Not anymore. – melpomene Apr 08 '19 at 18:39

2 Answers2

8

It specifies the minimum width that the output should be. If the width specified is more than the size of the output itself (the opposite of your case), then the difference between the length of your output and the width specified will be printed as spaces before your output.

So for example, printf("%2d\n", 12345); will just print 12345, since 12345 has 5 digits, but the width specified is only 2.

However, printf("%10d\n", 12345); will print _____12345 (5 spaces before 12345) since the difference between the width exceeds the length of the output by 5.

BusyProgrammer
  • 2,783
  • 5
  • 18
  • 31
  • So wouldn't it be better, then, to say "it specifies the *minimum* width of the output"? That emphasizes the detail that seems to be confusing the OP. – John Bollinger Apr 08 '19 at 18:24
1

Per the C standard, this number is the minimum field width. If the converted value has fewer characters than the minimum field with, it is padded with spaces. If it has the same number or more, the entire converted value is printed—it is not reduced to the given width.

Quoting from C 2018 7.21.6.1 4:

An optional minimum field width. If the converted value has fewer characters than the field width, it is padded with spaces (by default) on the left (or right, if the left adjustment flag, described later, has been given) to the field width. The field width takes the form of an asterisk * (described later) or a nonnegative decimal integer.

That is just part of paragraph 4. In its entirety, it gives the entire format of a conversion specification, which is a % followed by:

— Zero or more flags (in any order) that modify the meaning of the conversion specification.

— An optional minimum field width. If the converted value has fewer characters than the field width, it is padded with spaces (by default) on the left (or right, if the left adjustment flag, described later, has been given) to the field width. The field width takes the form of an asterisk * (described later) or a nonnegative decimal integer.

— An optional precision that gives the minimum number of digits to appear for the d, i, o, u, x, and X conversions, the number of digits to appear after the decimal-point character for a, A, e, E, f, and F conversions, the maximum number of significant digits for the g and G conversions, or the maximum number of bytes to be written for s conversions. The precision takes the form of a period (.) followed either by an asterisk * (described later) or by an optional nonnegative decimal integer; if only the period is specified, the precision is taken as zero. If a precision appears with any other conversion specifier, the behavior is undefined.

— An optional length modifier that specifies the size of the argument.

— A conversion specifier character that specifies the type of conversion to be applied.

Community
  • 1
  • 1
Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312