The difference between%02x and%2x

Source: Internet
Author: User

Output minimum width
A decimal integer to represent the minimum number of digits of the output. If the actual number is more than the defined width, the actual digit is output, or a space or 0 if the actual number is less than the defined width (when the minimum width value begins with 0).

X indicates that output 02 in 16 is
less than two digits, the front is 0 output, and if more than two digits, the actual output is an
example:
printf ("%02x", 0x345);  Print out: 345
printf ("%02x", 0x6);//Print out: A

and if directly written as%2X, the data is less than two bits, the actual output, that is, no additional 0 output, if more than two bits, the actual output.
printf ("%2x", 0x345);  Print out: 345
printf ("%2x", 0x6);//Print out: 6


Example:


#include <stdio.h>
int main ()
{
int i =-20;
for (i = -20; i < i++)
{
printf ("%2x\n", I);
printf ("%02x\n", I);
}
return 0;
}

Output results:

Ffffffec
Ffffffec

Ffffffed
Ffffffed

Ffffffee
Ffffffee

Ffffffef
Ffffffef

Fffffff0
Fffffff0

Fffffff1
Fffffff1

Fffffff2
Fffffff2

Fffffff3
Fffffff3

Fffffff4
Fffffff4

Fffffff5
Fffffff5

Fffffff6
Fffffff6

Fffffff7
Fffffff7

Fffffff8
Fffffff8

Fffffff9
Fffffff9

Fffffffa
Fffffffa

Fffffffb
Fffffffb

Fffffffc
Fffffffc

Fffffffd
Fffffffd

Fffffffe
Fffffffe

Ffffffff
Ffffffff

0
00

1
01

2
02

3
03

4
04

5
05

6
06

7
07

8
08

9
09

A
0a

B
0b

C
0c

D
0d

E
0e

F
0f

10
10

11
11

12
12

13
13

14
14

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.