Search code examples
c++castingunsigned-integer

c++ Need for typecasting int to unsigned int


I had this code as part of a C++ project

unsigned int fn() {
    //do some computations
    int value = ....

    if(value <= 0)
        return 0;
    return (unsigned int)value;
}

I don't get the need to use the cast at the last return statement since all negative numbers will be caught in the if statement(hence returning 0).

And moreover this function fn is called from another function (whose return type is int) that simply returns the value returned by fn.

Thanks..


Solution

  • The purpose is to silence the compiler warning that could otherwise be issued.