1

I have the following C program (as simple as it gets):

#include <stdio.h>

main()
{
printf("Test");
}

But using the gcc compiler within Cygwin, I cannot get this program to work unless I make the following modifications:

#include <stdio.h>

int main()
{
    printf("Test");
    return 0;
}

Could anyone explain why? What is so special about the "int" and the "return 0;" ?

Thanks! Amit

6
  • What version of GCC is that? While leaving out return 0 in main is incorrect in C (as opposed to C++), leaving out the return type should make it int by default. Your first code compiles successfully (but with warnings) on MinGW GCC 4.5 in -ansi, -std=c90 and -std=c99 modes. Commented Jan 5, 2011 at 11:34
  • @Kos: What do you mean by "(as opposed to C++)"? Commented Jan 5, 2011 at 11:36
  • In C++ you can "legally" omit return 0; in main() IIRC - a standards-aware compiler is then required to make it behave as if there was a return 0;. Not that it's important... Commented Jan 5, 2011 at 11:38
  • @Kos: And this is also true in C. Commented Jan 5, 2011 at 11:38
  • @Charles - hmm... but only in C99, right? My gcc gives a warning in C90 mode, but indeed not in C99. Commented Jan 5, 2011 at 14:01

4 Answers 4

2

With C you are always required to specify your output type. So the int is always required (with some compilers, void will work too).

The "normal" minimal version is this:

int main(int argc, char **argv){
    return 0;
}

Depending on your system you could also have a char **envp and char **apple there aswell.

http://en.wikipedia.org/wiki/Main_function_(programming)#C_and_C.2B.2B

Sign up to request clarification or add additional context in comments.

8 Comments

If you say minimal, you don't need the argc and argv arguments ;).
Also void is not an allowed return type in any C or C++ standard.
@rubenvb: I was talking about the standards, in that case the argc and argv are required.
@Kos: True, I have not claimed that it is allowed by any standard. I'm just saying that it works with some compilers ;)
@WoLpH: Not true. int main(void) must be accepted by conforming C (and C++) compilers.
|
1

int is the required return type for main per the C standard.

3 Comments

return 0; doesn't prevent main from returning an unspecified values because "[...] reaching the } that terminates the main function returns a value of 0." (ISO/IEC 9899:1999 5.1.2.2.3)
@Charles, I didn't know that. Updated my answer. Is the same true in C++, btw?
Yes, in C++ "If control reaches the end of main without encountering a return statement, the effect is that of executing return 0;" (ISO/IEC 14882:2003 3.6.1 [basic.start.main] / 5)
0

The int is a return code. I believe C defaults to returning 0. Generally speaking a return of 0 means successful completion of program while 1,2,3... would be error codes. It is often used by programs that want to test if the program ran successfully.

Comments

0

intis the return type for the function, in this case the main(). As you know, you always need to specify the return type of a function in C (it could be void; there you don't need to return anything)!

When your function has a return type (int, for example), you are obligated to put a return sentence (return 0;, for example). In C, is a "standard" to return 0 when a function executed correctly. Also, when you run your program, you can obtain the value returned after the execution (in this case it will be 0). If the execution of your program would terminate erroneously, then you could return any other value (-1, 1, 2, etc.) and it is easier for you to detect the error and debug it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.