U in variable declaration

I know that if the number is followed by a U-suffix, it is treated as unsigned. But why does the following program print the correct value of the variable i, even if it is initialized with a negative value. (Compiled with gcc 4.9.2, 4.8.2 and 4.7.1)

Program1.cpp

#include <iostream>
int main()
{
    int i=-5U;
    std::cout<<i;  // prints -5 on gcc 4.9.2, 4.8.2, & 4.7.1
}

Program2.cpp

#include <iostream>
int main()
{
    auto i=-5U;
    std::cout<<i;  // prints large positive number as output
}

But if I use the auto keyword (deductor type new C ++ 0x), it gives me a large positive number as expected.

Please correct me if I misunderstand something.

+4
source share
1 answer

-5U not -5 U. That -(5U). The minus sign is the negation operator that acts on 5U, and not the first character, an integer literal.

, 2^n, n - . . , ( , int), , undefined, * , - , , , , , .

. * . , " undefined" . , ( ), , -.

+11

All Articles