Little bit more on the return type of main just for fun...
For x86 systems.... the return value of a function is generally found inside the EAX register of the CPU. That means whatever is found inside EAX will be your return value for main() as well as any function that you call.
Needless to say, that is not the only purpose of the EAX register, and it will/can be overwritten for other basic calculations and such, but one of the side effects of this is that if you call a function right before main() ends, you could implicitly be passing the return value of another function as main()'s return value. If that function returns void, the fun gets even better.
Proof of concept? Take the following three programs:
Code:
#include <stdio.h>
void main()
{
printf("Bleh!\n");
}
Code:
#include <stdio.h>
int main()
{
int iRet;
printf("Enter a return value for this program: ");
scanf("%d",&iRet);
return iRet;
}
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int getProgram(char *, size_t);
int main()
{
int iRet;
char szProgram[256];
while(getProgram(szProgram,sizeof(szProgram)))
{
iRet = system(szProgram);
printf("Return value of \"%s\":\t%d\n\n",szProgram,iRet);
}
return 0;
}
int getProgram(char *szProgram, size_t size)
{
int iLength;
printf("Enter a program name or \"exit\" to leave: ");
if(!fgets(szProgram,size,stdin))
{
fprintf(stderr,"Error reading from stdin.\n");
return 0;
}
iLength = strlen(szProgram);
if(szProgram[iLength-1] == '\n')
{
szProgram[iLength-1] = '\0';
}
return strcmp(szProgram,"exit");
}
The first program is rather obvious, as is the second. The third program keeps asking the user for a program to run, passes the argument to the shell, and then receives the return value from the shell, which will be the return value of the program that you run.
To see my point, you could run the last program and input the name of the first program. You should get a return value of 6. Why? Because printf() returns the amount of chars that it printed, which in this case is 6 chars, 'B', 'l', 'e', 'h', '!', and '\n'. This return value is stored in the EAX register regardless of whether or not you need it or use it.
Since there is no explicit return in main() in the first program, and no other instructions, the return value of printf() just sits there collecting dust until your program ends. The shell then takes that same value in EAX as the value that you intended to return from main() since it doesn't give a care how you declared main(). It just knows to that you're supposed to give it a number.
For a program that simple, the return value is guaranteed to be constant. For some programs, however, it's totally possible that the last value inside EAX could be "random" depending upon various factors, such as how your program is run. If you run the second program, obviously, whatever you give to scanf() will be returned, value or invalid.
For example, I ran the last program and when prompted to run a program, I chose to run the second one. When then prompted to enter a value, I entered 'x', which is obviously not a valid integer. The return value was 4231228.
Even if you never use the return value, imo, it is a good idea to get into the habit of declaring main() as returning an integer and explicitly returning a value. This way, you can actually make use of the return value and signify if any errors occurred in your program or if the user can expect that everything went well.