Char unsigned char % d % u

Source: Internet
Author: User

I encountered such a problem a few days ago. When learning the difference between unsigned char and signed Char, whether the char type is signed or unsigned depends on the compiler.

At that time, I wrote a series of Code as follows:

char a=-1;    printf("%d\n",sizeof(a));    printf("%d\n",a);    printf("%u\n",a);

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/4C/6E/wKiom1Q9MVPwoAU0AAAg9woIWUE769.jpg "Title =" qq 51114221943.jpg "alt =" wkiom1q9mvpwoau0aaag9woiwue769.jpg "/>

First, % d is used to output data in int decimal format;

% U is to output data in the unsigned int decimal format;

So why does it output 4294967295? What's more, the char type only occupies one byte, and the result is clearly the maximum integer of four bytes!

For this reason, we also specially checked the memory status as follows:

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/4C/6E/wKioL1Q9Mr6z1YtWAABSyekhf2U060.jpg "Title =" &a4.jpg "alt =" wkiol1q9mr6z1ytwaabsyekhf2u060.jpg "/>


650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/4C/6E/wKiom1Q9MonQ1276AACigK7RXg8822.jpg "Title =" memary4.jpg "alt =" wkiom1q9monq1276aacigk7rxg8822.jpg "/>

After querying, CC indicates a random number in the memory.

This makes it harder for me to understand: Why is the maximum int integer of four bytes output?


At that time, my Baidu was the difference between % d and % u. Later I knew that my direction was wrong, and the cause of this result was

The difference between the printf function for unsigned and signed Char.


1. In printf, the output in the format of % d is to read out the memory blocks of the elements in signed form, such as char a =-1;

The memory is 0xff, and this number is marked with-1 (the relationship between the original code, the reverse code, and the complement code is described)


When printf uses the % u format, if the number of read objects is of the unsigned type, there will be no "character expansion". If it is of the signed type (char short int long) there will be character extensions, which cannot be viewed in the memory and are extended into four bytes of data, as shown below:

If the data to be read is smaller than 0, the symbol bit in the memory is 1. During expansion, the number of less than 32 digits must be supplemented by 1 (because the first part is 1, similar to the <> shift left or right method), the extended number is then read in unsigned int decimal format. For example, in this example, the value is-1, the memory is 0xff, and the extended value is 0xff FF. Then, the read duration is 4294967295.

If the value is 1, the memory is 0x01 (the first is 0). Therefore, the extended value is 0, that is, 0x00 00 01, and the read result is also 1.

#include<stdio.h>int main(){        char c1=1,c2=-1;    printf("%d\n",c1);    printf("%d\n",c2);    printf("%u\n",c1);    printf("%u\n",c2);    printf("#####################\n");    long l1=1,l2=-1;    printf("%d\n",l1);    printf("%d\n",l2);    printf("%u\n",l1);    printf("%u\n",l2);    printf("#####################\n");    unsigned char c3=1,c4=-1;    printf("%d\n",c3);    printf("%d\n",c4);    printf("%u\n",c3);    printf("%u\n",c4);    printf("#####################\n");    unsigned long l3=1,l4=-1;    printf("%d\n",sizeof(l4));    printf("%d\n",l3);    printf("%d\n",l4);    printf("%u\n",l3);    printf("%u\n",l4);    return 0;}

Running result:

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/4C/6E/wKiom1Q9OiOQ-TSWAACc6Ig0n9s972.jpg "Title =" knot 3.jpg "alt =" wKiom1Q9OiOQ-TSWAACc6Ig0n9s972.jpg "/>

Note: The final unsigned long L4 =-1; when output at % u, the result is also 4294967295, but it is not the result of character extension, because it originally occupies 4 bytes and is 0xff FF in the memory, the output is similar to the character extension.

This article is from the "xingnallu" blog, please be sure to keep this source http://781588100.blog.51cto.com/9429625/1564128

Char unsigned char % d % u

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.