Changed c++ unsigned int from 0 to -2, but prints 4294967294

273 views Asked by At

I am taking a Microsoft Course called Introduction to C++ on edx.org. It's a good course. Instead of just breezing through, I am trying to understand as much as possible. Below is the part of this video that I do not understand. I don't understand why unsigned int u{ 0 }; outputs 0, but when I change it to u = -2;, it outputs 4294967294.

    #include <iostream>

    int main()
    {

    unsigned int u{ 0 };
    std::cout << u << std::endl;
    u = -2; 
    std::cout << u << std::endl; // prints 4294967294 as u?

    return 0;
    }

Thank you for your time in advance.

0

There are 0 answers