tags:

views:

11341

answers:

12

What way is the most efficient and why? int main()? void main()? return 1? return 0?

+53  A: 

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.

workmad3
main CAN be entered/left multiple times, but that program probably wouldn't win any design awards ;)
korona
C99 also has the C++ mis-feature that reaching the end of the main() function is equivalent to returning 0 -- if main() is defined to return a type compatible with int (section 5.1.2.2.3).
Jonathan Leffler
reentering main is not valid C++. Explicitly in the standard, 3.6.1.3 states 'main shall not be used within a program'
workmad3
stdlib.h provides EXIT_SUCCESS and EXIT_FAILURE for this purpose
Clay
@korona: using main() from inside the code is undefined behavior. As the compiler may(depending on compiler) insert code to handle program initialization at this point. Running the initialization code twice may not be a good idea.
Martin York
0 and non-zero are correct but entirely meaningless to someone reading your code. This question is proof that people don't know what valid/invalid codes are. EXIT_SUCCESS/EXIT_FAILURE are much more clear.
JaredPar
JaredPar: returning 0 from main is clearly defined in C as a successful termination. Martin York: calling main() from inside a C program is certainly _not_ undefined behavior. I cannot say the same for C++.
Chris Young
As said, for C++ calling main() is explicitly stated in the standard as illegal (which is a step worse than undefined). For C though, the case is obviously different (but probably best avoided ;))
workmad3
It may be clearly defined in the C standard but it's definately not clearly common knowledge. Otherwise this question wouldn't have been posted.
JaredPar
JaredPar: well, it's common knowledge among programmers that know C, and usually I'd expect people reading C source to understand C.
Chris Young
@workmad3, notice this defect report though about using main for GCC: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=41431 . Also notice the following for allowed definitions of main: http://groups.google.com/group/comp.std.c++/browse_thread/thread/a1e5504b499bc58f?pli=1
Johannes Schaub - litb
+11  A: 

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.

Lou Franco
A: 

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).

Daren Thomas
+5  A: 

The standard says main should return an int. void is not supposed to work. Take a look at this question, too.

xtofl
+12  A: 

I believe that main() should return either EXIT_SUCCESS or EXIT_FAILURE. They are defined in stdlib.h

dmityugov
http://www.gnu.org/software/libc/manual/html_mono/libc.html#Exit-Status
dmityugov
EXIT_SUCCESS and EXIT_FAILURE are standard. Have been since 1989.
Michael Burr
0 is also standard.
Chris Young
+2  A: 

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.

graham.reeds
+3  A: 

Keep in mind that,even though you're returning an int, some OSes (Windows) truncate the returned value to a single byte (0-255).

Ferruccio
Unix does the same, as do most other operating systems probably. I know VMS does such incredible weird things with it that returning anything other than EXIT_SUCCESS or EXIT_FAILURE is asking for trouble.
Leon Timmermans
+10  A: 

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.
Chris Young
A: 

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!!!!!!!!

Ranjeeth P T
A: 

what will be the output ...if we write

RETURN 1

in C programming.....I mean what its mean...

Mitali Halder
A: 

in c main() does not return any value not even void.

tamin
A: 

c main return is optional, c++ main must return integer value

sakthivel