Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does main() require braces?

I tried several variations on

main() return;

or

main() if();

and obtained different errors, the most peculiar of which was

/usr/lib/gcc/i686-linux-gnu/4.4.5/../../../../lib/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status

While it's uncommon for a program to require only one statement, why does main() make it a requirement to have braces?

Could someone explain why the error was so peculiar when compiling just int main();?

like image 840
user490735 Avatar asked May 18 '26 23:05

user490735


1 Answers

Because you are defining a function named main() and a function definition is basically a function declaration (the int main() part) followed by a compound statement (the { /* ... */ } part) (you could also use a function try block, but those are very rarely used and still require braces).

You can't define any function without braces.

like image 151
James McNellis Avatar answered May 21 '26 13:05

James McNellis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!