The difference between int,signed int and unsigned int and printf%u,%d

Source: Internet
Author: User
############## #首先 ###############
Signed int signed integral type
unsigned int non-symbolic integer
int is signed int, which is written with [signed] int to indicate signed can omit

2^31=2147483648
2^31-1=2147483647
2^32=4294967296
2^32-1=4294967295

############## #对有符号整型 ###############
range from-2147483648 to -1=2^31 = 2,147,483,648
The binary is expressed as a complement, with the highest bit 1 beginning:
1000 0000 0000 0000 0000 0000 0000 0000->-2147483648
1000 0000 0000 0000 0000 0000 0000 0001->-2147483647
...
1111 1111 1111 1111 1111 1111 1111 1110->-2
1111 1111 1111 1111 1111 1111 1111 1111->-1

range from 0 to 2147483647 =2^31 = 2,147,483,648
The binary uses the complement (the positive source code inverse code complement) to indicate that the highest bit 0 begins:
0000 0000 0000 0000 0000 0000 0000 0000-> 0
0000 0000 0000 0000 0000 0000 0000 0001-> 1
...
0111 1111 1111 1111 1111 1111 1111 1110-> 2147483646
0111 1111 1111 1111 1111 1111 1111 1111-> 2147483647

############## #对无符号整型 ###############
Unsigned is the highest bit does not represent a sign bit, still used as a numeric bit
range from 0 to 4294967295=2^32 = 4,294,967,296

############## #关于printf的打印 ###############
%d signed
%u no sign

Signed int i =-3; That is i=11111111111111111111111111111101 (signed number 3)
unsigned int i2 = -13;//= i=11111111111111111111111111110011 (signed number-13)
(1)
printf ("I's value is%d\n", i);//Parse I to a signed integer, output: I has a value of-3
printf ("I2 value is%d\n", i2);//Parse I to a signed integer, output: The value of i2 is-13
(2)
printf ("I's value is%u\n", i);//Parse I to a signed integer, output: I has a value of 4294967293 (that is, =4294967295-2)
printf ("I2 value is%u\n", i2);//Parse I to signed integer, Output: I2 value is 4294967283 (that is, =4294967295-12)

for (1) is signed as-is parsing, the highest bit is the symbol bit
for (2) is unsigned parsing, the highest bit is treated as a numerical bit


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.