What way is the most efficient and why? int main()? void main()? return 1? return 0?
the return value for main should indicate how the program exited. Normal exit is generally represented by a 0 return value from main. Abnormal termination is usually signalled by a non-zero return but there is no standard for how non-zero codes are interpreted. Also as noted by others, void main() is explicitly prohibited by the C++ standard and shouldn't be used. The valid C++ main signatures are:
int main()
and
int main(int argc, char* argv[])
although many will also accept
int main(int argc, char** argv)
It's also worth noting that in C++, int main() can be left without a return value at which point it defaults to returning 0. This is also true with a C99 program. Whether return 0 should be omitted or not is open to debate. The range of valid C program main signatures is much greater.
Also, efficiency is not an issue with the main function. It can only be entered and left once (marking program start and termination) according to the C++ standard. For C, the case is different and re-entering main() is allowed, but should probably be avoided.
Return 0 on success and non-zero for error. This is the standard used by UNIX and DOS scripting to find out what happened with your program.
This basically depends on your execution environment (the OS). C implies that it will be run by a UNIX like OS which expects the program to return a (small? 1 Byte? can't remember) integer to indicate success / failure.
You should probably just use int main(int argc, char** argv)
.
The standard says main
should return an int. void
is not supposed to work. Take a look at this question, too.
I believe that main() should return either EXIT_SUCCESS or EXIT_FAILURE. They are defined in stdlib.h
I was under the impression that standard specifies that main doesn't need a return value as a successful return was OS based (zero in one could be either a success or a failure in another), therefore the absence of return was a cue for the compiler to insert the successful return itself.
However I usually return 0.
Keep in mind that,even though you're returning an int, some OSes (Windows) truncate the returned value to a single byte (0-255).
The accepted answer appears to be targetted for C++, so I thought I'd add an answer that pertains to C, and this differs in a few ways.
ISO/IEC 9899:1989 (C90):
main should be declared as either:
int main(void)
int main(int argc, char **argv)
Or equivalent. For example, int main(int argc, char *argv[]) is equivalent to the second one. Further, the int return type can be omitted as it is a default.
If an implementation permits it, main can be declared in other ways, but this makes the program implementation defined, and no longer strictly conforming.
The standard defines 3 values for returning that are strictly conforming (that is, does not rely on implementation defined behaviour): 0 and EXIT_SUCCESS for a successful termination, and EXIT_FAILURE for an unsuccessful termination. Any other values are non-standard and implementation defined. main must have an explicit return statement at the end to avoid undefined behaviour.
Finally, there is nothing wrong from a standards point of view with calling main() from a program.
ISO/IEC 9899:1999 (C99):
For C99, everything is the same as above except:
- The int return type may not be omitted.
- You may omit the return statement from main. If you do, and main finished, there is an implicit return 0.
main() alwayz returns int bet...
try this in linux machine
int main() {
return 1;
}
the question wer did 1 go ??
ans: to shell
in shell type echo $? you can see 1.
Now change the code
void main() {
}
now type echo $? in shell you can see an integer!!!!!!!!
what will be the output ...if we write
RETURN 1
in C programming.....I mean what its mean...