Whole promotion signed / unsigned and printf

I looked at C ++ Integer Overflow and Promotion , tried to replicate it, and finally ended up with this:

#include <iostream>
#include <stdio.h>

using namespace std;

int main() {
    int i = -15;
    unsigned int j = 10; 
    cout << i+j << endl; // 4294967291
    printf("%d\n", i+j); // -5 (!)
    printf("%u\n", i+j); // 4294967291

    return 0;
}

coutdoes what I expected after reading the message mentioned above, like the second printf: both print 4294967291. The first printf, however, prints -5. Now I assume that this printfsimply interprets the value of unsigned 4294967291 as a signed value ending in -5 (which would correspond to the fact that 2 complement 4294967291 is 11 ... 11011), but I'm not 100% sure that I missed nothing. So am I right or is something else happening here?

+5
source share
1 answer

, . printf() : , .

+4

All Articles