Search code examples
cconsole-applicationreturn-valueprogram-entry-pointnegative-number

Returning "-1" from int main shows "return value 4294967295" in the console why?


I am using Windows-11, 64-bit, and Dev-C++ (Bloodshed v5.11); the following is the simple code snippet:-

#include<stdio.h>

int main()
{
    return -1;
}

I am getting the following output in the console:-


Process exited after 0.2015 seconds with return value 4294967295 Press any key to continue . . .

In the output, I do recognize that 4294967295 (2^32 - 1) is the max value of the unsigned 4-byte integer, but I cannot understand why this value is returned instead of "-1".


Solution

  • -1 in two's complement signed number format (assuming 4 bytes long integers) is 0xffffffff which is the maximum unsigned value that can be stored in the 4 bytes. Two's complement format is used by almost all computers used nowadays (including your PC, Mac and any mobile phone)

    Your shell is displaying the program return value as unsigned integer. The decimal representation of the 0xffffffff hex number is 4294967295