Symbol number, unsigned number, integer overflow in C language

Source: Internet
Author: User

[CPP]View Plaincopy print?
    1. #include <stdio.h>
    2. void Main ()
    3. {
    4. int l=-1;
    5. unsigned int c=135;
    6. printf ("%u\n", l+c);
    7. }

The result of this is 134, instead of the large positive number I thought before, it is actually important to note that -1 (0xFFFFFFFF) is promoted to unsigned int and is a 1 unsigned int that overflows, so the result is 134 after the addition. But if l=-10000, then the result is really a big positive number, because it does not involve overflow.
There are two kinds of integer arithmetic operations in C language, symbolic type and unsigned type. Unsigned number operations: All unsigned number operations are modulo 2 N, (n is the number of digits in the result). So there is no such thing as an "overflow" when it is not in operation, and when it goes beyond the range, it is counted from zero! When an unsigned number and a signed number are calculated, the signed number is automatically converted to an unsigned number to participate in the operation! Signed number operation: "Overflow" is possible, and the result of "overflow" is not fixed.

About the usage error of unsigned number minus unsigned number:

if (i-j >=0) If I,j is an unsigned number, this write may cause an error, that is, when I is less than J, the formula is still true because the unsigned number is always greater than or equal to zero. Example: if (strlen (a) >= 10) and if (strlen (b) -10 >= 0) These two statements are not equal because the Strlen function returns the unsigned number type.

March 8, 2015 Add a point: Today look at this place some do not understand in the online search for information found that the unsigned number subtraction, if the meiosis is less than the meiosis, then the result will be a very large number of unsigned, rather than an imaginary signed number. Therefore, it is necessary to make a judgment before subtracting the unsigned number, preferably by using the if (strlen (a) >= 10) method when comparing, instead of using the IF (strlen (b) -10 >= 0). Since the result of an unsigned number is an unsigned number, the signed number is coerced to an unsigned number when the unsigned number and the signed number are computed.

For example, the following example is interesting:

[CPP]View Plaincopy print?
    1. #include <stdio.h>
    2. int main ()
    3. {
    4. unsigned int a=6;
    5. int b=-20;
    6. printf ("%d\n", (a+b) >0);
    7. }

This small example can be tried by the machine. There are also a few good questions I also attached to the last.

It is important to note that I found a small problem when I was writing the program:


So unsigned char is subtracted from signed int?

[CPP]View Plaincopy print?
    1. #include <stdio.h>
    2. void Main ()
    3. {
    4. unsigned char A;
    5. int b;
    6. a=0;
    7. b=2;
    8. printf ("x=%d\n", (A-B) >0);
    9. }

ANSI c specifies that bit patterns should not change when casting between unsigned integers and signed integers. The type conversion does not alter the object's bit pattern, but it changes the way the bit pattern is interpreted.

When a signed number is converted to an unsigned number, negative numbers are converted to large positive numbers (which can be understood as the original value plus 2 n-th), while positive numbers remain unchanged.

When an unsigned number is converted to a signed number, the original value is maintained for the small number, and the large number is converted to a negative value (which can be understood as the n-th square minus 2).


When I look at C tonight, I suddenly think of what it would be like if you gave that type variable a range of values greater than unsigned int.

[CPP]View Plaincopy print?
    1. void Main ()
    2. {
    3. unsigned int A;
    4. a=7000000000;
    5. printf ("a=%d\n", a);
    6. printf ("a=%u\n", a):
    7. }

The two results are completely different.


A few small examples:

Topic One:

int a =-1;

unsigned int b = 1;

printf ("%d", a > B);

Result output: 1

Because the unsigned number is compared with the signed number, the signed number is converted to the unsigned number and then compared. A converted to an unsigned number is 0xFFFFFFFF, definitely greater than B

Topic Two:

char a =-1;

unsigned char b = 1;

printf ("%d", a > B);

Result output: 0

Strange, how could this be? This is because the two are promoted to int, and a is promoted to int, which means that -1,B is promoted to int or 1, which is less than the latter. Note that it's not as simple as the title one to convert char to unsigned char.

Topic Three:

int a =-1;

unsigned char b =-1;

printf ("%d\n", a < b);

The result output is: 1

The reason is that to promote B to int is 255 of course greater than-1, unsigned char first promoted to int, into the low eight bits of int, high all 0.

Topic Four:

char a =-1;

unsigned int b =-1;

printf ("%d\n", a = = B);

Result output: 1

The reason is that the char type is extended to unsigned int and is equal to B, same as 0xFFFFFFFF

An elevation of less than int to Int,int is from signed to unsigned

For floating-point numbers, floating-point numbers (float,double) are actually signed, and the unsigned and signed prefixes cannot be added to the float and double, and there is no question of the conversion between the signed number root unsigned numbers.

Be sure to remember that if you need to use a signed number, do not forget to cast

Symbol number, unsigned number, integer overflow in C language (RPM)

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.