Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

main() function defined without return type gives warning

This is my program:

main()
{ 
    printf("hello world\n");
}

I get this warning when compiling it:

function should return a value

When changing main() to void main(), the warning disappears.

Why is that so?

like image 794
user2669749 Avatar asked Feb 13 '26 11:02

user2669749


1 Answers

There are few things which you should take note of :

  1. The int is the main() function's return type. That means that the kind of value main() can return is an integer.
  2. main( ) was tolerated by the C90 compilers but not by C99 compilers which means its not a part of C99 standard anymore , so don't do this.
  3. void main() is not a standard form ,some compilers allow this, but none of the standards have ever listed it as an option. Therefore, compilers don't have to accept this form, and several don't. Again, stick to the standard form, and you won't run into problems if you move a program from one compiler to another.
  4. And one last thing , instead of writing main like this :

    int main() // here you are being silent about passing arguments to main , meaning it may or may not take arguments

write like this :

int main(void)// this specifies there are no arguments taken by main

You might wanna look at the C99 standard for further details.

like image 97
0decimal0 Avatar answered Feb 15 '26 01:02

0decimal0



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!