-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
  • 205,898
  • 23
  • 218
  • 273
l2l
  • 17

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
  • 205,898
  • 23
  • 218
  • 273