简体   繁体   中英

printf("%2d") in C programming

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

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM