2

Hello programming world.

I am currently doing my first programming course at university and our instructor said the function

int main (); //without the curly braces// is valid. 

I may have misheard him/misinterpreted him, as when I try and run a console with that, it gives an error. But when I do int main() {}; it runs fine. So: 1. Are the curly braces needed regardless of the content in the body?

  1. How did the function run without the return 0.

  2. Using this, what is the shortest possible int main / void main function possible?

as requested, here is the error:

Undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Thank you so much :)

Xara
  • 8,748
  • 16
  • 52
  • 82
  • `int main();` *declares* but doesn't *define* `main`. To *define* `main`, you need minimally, `int main() {}`. That is, you need a function body which is delimited by curly braces (`{}`). – lurker Feb 14 '14 at 20:22
  • What does "it gives an error" mean? Tell us (by updating the question, not by replying to this comment) what the error is. If there's an error message, copy-and-paste it into your question. – Keith Thompson Feb 14 '14 at 20:24

4 Answers4

4

In C++, there are two correct definitions for main:

int main() {
    // ...
}

and

 int main(int argc, char *argv[]) {
     // ...
 }

or equivalent. (Other implementation-defined forms are possible, but we can ignore those. And the return type is always int, at least for hosted implementations; void main() is wrong.)

The braces { and } are part of the syntax of a function definition; they're not optional.

You can provide a declaration for main, such as

int main();

but there's no real point in doing so. That's a declaration, not a definition -- and you still need to have a definition somewhere.

A return 0; at the end is not required. This is a special case that applies only to main, not to other functions: if execution reaches the closing }, it does an implicit return 0;.

The shortest legal program would probably be:

int main(){}
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
0

Thats the difference between a function definition and declaration (see What is the difference between a definition and a declaration?

Basically int main(); is a prototype telling the compiler that you will have a function called main, which returns an int, but you do not implement it yet.

the int main() {} is the implementation of the function, thus the curly braces, giving it a function body and full implementation.

Community
  • 1
  • 1
MatthiasB
  • 1,759
  • 8
  • 18
  • a definition is the operations within the main. a declaration is telling the compiler there will a 'main' at somepoint so it should prepare itself. – 173901 Feb 14 '14 at 20:27
0

I'd like to clarify a few things.

int main();

is a function declaration, i.e. it lets other functions / classes know about it.
However, it does not define main, meaning it says nothing about what main actually does.
Since every C++ program must define main, as it is run first, your compiler will definitely give a compile error.

By writing

int main() {}

You are defining main by specifying that main does nothing, so it will run.

Finally, C++ compilers will implicitly add a return 0; statement if you do not return anything, as it is an indicator to the operating system that the program ran successfully.

For more information, see https://stackoverflow.com/a/204483/2512775 on what main should return.

Community
  • 1
  • 1
James Zhu
  • 140
  • 1
  • 6
0

Your error code means that you have not declared the main() function properly. What you should do is add the curly braces to signify the block of code that your application will run in.

Although the compiler will add a return statement if it isn't given one, just add one to make sure.

Adil Patel
  • 171
  • 1
  • 11