What does [C/C + +]% 2d,% 02d mean

1. % d is normal output

2. % 2d is to output the number with width of 2 and right alignment. If the number of data bits is less than 2, fill in a space on the left:

3. % 02d is similar to% 2d, but 0 is added on the left

Modifier format description meaning:
1,M

%md outputs an integer number with width M. if it is less than m, the left space is filled

2,0m

%0md outputs the integer number with width M. if it is less than m, fill zero on the left

3,m,n

%m.nf outputs real decimals with width m and n decimal places

%2d,%-2d,%.2d,%02d

int main()
{
	int a = 1;
	printf("%d\n",a);
	printf("%2d\n",a);
	printf("%.2d\n", a);
	printf("%-2d\n",a);
	printf("%02d\n",a);
	return 0;
}

   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10


As shown in the figure, 2 represents the output width. When it is greater than the output width, the data is output according to the original data. When it is less than the output width of 2, it is preceded by a space by default. If there is a - sign, it means to fill in a space after. 2d is the same as 02d, which means to fill in 0 before the width is less than 2

%md is similar and will not be repeated

%2x, %02x, %-2x, %.2x

X means output in hexadecimal
02 indicates less than two bits, and 0 is added in front of it for output; If more than two bits, the actual output

#include<stdio.h>
int main()
{
	printf("%02X\n", 0x325);  
	printf("%02X", 0x5); 
	return 0;
}

   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

If it is written as% 2x, if the data is less than two digits, it will be output with spaces in front, and no additional 0 will be output; If more than two bits, the actual output

If it is written as% - 2x, if the data is less than two digits, it will be output with spaces after it, and no additional 0 will be output; If more than two bits, the actual output

If written as%. 2x, the effect is the same as% 02x

#include<stdio.h>
int main()
{
	printf("%2X\n", 0x325);  
	printf("%2X\n", 0x5); 
	printf("%-2X", 0x5);
	printf("1");//Space after validation
	return 0;
}

   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9


%mx is similar and will not be repeated

%f, %.nf, %m.nf,

#include<stdio.h>
int main()
{
	double num;
num <span class="token operator">=</span> <span class="token number">3.1415</span><span class="token punctuation">;</span>
<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%f\n"</span><span class="token punctuation">,</span>num<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%5.2lf\n"</span><span class="token punctuation">,</span> num<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%.2lf\n"</span><span class="token punctuation">,</span> num<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%.6lf\n"</span><span class="token punctuation">,</span> num<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13


1.%f does not limit the width and the number of digits after the decimal point
2.%mf indicates that the data output width is limited to m, and this data width also includes the decimal point
Fill in the space before the insufficient width. Data exceeding the width is output normally.
eg:%5.2lf -- 3.14 accounts for 4, and the width is less than 5, so the space is filled in front.
3.%.nf -- indicates that two decimal places are reserved after the decimal point.

%e, %.ne,%m.ne,

%e: Print numbers as exponents
When the output width is not specified, the default number part has 6 decimal places and the index occupies 4 digits.
Note: the decimal part does not count as the decimal point, and both index e and + / - count as the number of index digits.


%m.ne:m indicates the number of output columns, and n indicates how many decimal places there are
When the number of output columns > m, output according to the original data
When < m, the preceding space is output

%-m.ne is followed by a space.

%. ne indicates that the number of output columns is not limited, and n decimal places are reserved.

int main()
{
	printf("%e\n",123.456);
	printf("%.2e\n", 123.456);
	printf("%13e\n",123.456);
	printf("%-13e", 123.456);
	printf("1\n");//Verify spaces
	printf("%3e\n", 123.456);
	printf("%13.2e\n", 123.456);
	return 0;
}

 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

Finally, this article is based on a large number of experiments. If you have any questions, please point out

Tags: C C++

Posted on Mon, 25 Oct 2021 10:27:48 -0400 by dreamwest