Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How does Variable > -1 exactly evaluate in C?

#include <stdio.h>
#include <stdint.h>
int main()
{
    uint16_t peter = 8;
    uint32_t peter2 = 8;
    if(peter > -1)
    {
        printf("Peter true\n"); //expected
    } 
    if (peter2 > -1)
    {
        printf("Peter 2 true\n"); //wtf, why not
    }

    return 0;
}

Why does the first statement enter the clause and the second does not (on a 32bit architecture)?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The explanation for the difference between peter > -1 and peter2 > -1 are the notions of “integer promotion”, and also “usual arithmetic conversions”.

Integer promotion” means that any integer type t narrower than int is “promoted” to either int (if int can contain all the values of t) or unsigned int otherwise. In your example and with your compiler, promotion is applied to peter.

Then, the “usual arithmetic conversions” decide what happen when an arithmetic binary operation, here >, is applied to operands of different types. Between an unsigned and a signed integer type of the same width, these rules say that the signed operand is converted to unsigned before the operation takes place.

-1 has type int. When it is compared to peter2, the usual arithmetic conversions mean -1 is converted to uint32_t, becoming a large value (greater than 8).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading