-2

I am reading The C Programming Language and the first program is to print Hello World I wrote the code as it shown in the book:

#include <stdio.h>

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

But I got an error warning: type specifier missing, defaults to 'int' [-Wimplicit-int] main() . I fixed it by writing the code like this:

#include <stdio.h>

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

Can anyone tell me what is the difference and why should I write it like that?

dbush
  • 162,826
  • 18
  • 167
  • 209
l2l
  • 17
  • 5
    C++ clan is at war with the C clan. The fragile truce is maintained by keeping the C++ and C tags separated. – Ron Oct 07 '17 at 18:31
  • @Ron I was always wandering why my bedroom looks like after a serious fight. Now I know - as I use both I have to have al least two personalities and they fight when I sleep. – 0___________ Oct 07 '17 at 18:47
  • 1
    @melpomene both were asked so many times - it is even not worth to comment - OP did not even use the google – 0___________ Oct 07 '17 at 18:48
  • @melpomene I't about the valid signature of `main` it's an exact duplicate – bolov Oct 07 '17 at 19:10
  • 1
    @melpomene fixed. You may remove the comment now :D – Antti Haapala Oct 07 '17 at 19:13
  • 1
    @bolov incorrect. `main()` with implicit `int` still returns `int`, and takes no arguments. – Antti Haapala Oct 07 '17 at 19:14
  • @AnttiHaapala there is an answer on the dupe dealing exactly with that: https://stackoverflow.com/a/31263079/2805305 – bolov Oct 07 '17 at 19:28

1 Answers1

0

Older versions of C had the concept of a default type. If a variable is declared without a type, it is assumed to be int. Similarly with functions, if a function is defined without a return type, it is also assumed to return int.

More recent versions of C (i.e. versions less than 25 years old) did away with default types and output a warning in this case. It's best to explicitly specify the type to avoid ambiguity and keep with more modern C.

dbush
  • 162,826
  • 18
  • 167
  • 209