Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
368 views
in Technique[技术] by (71.8m points)

c - main() function defined without return type gives warning

This is my program:

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...