1

Here's the code, straight from 'The C programming Language, Second edition'.

#include <stdio.h>

main ()

{

printf("hello, world\n");

}

Here's the GCC error:

user@root:~/bin$ gcc helloworld.c 
helloworld.c:2:1: warning: return type defaults to ‘int’ [-Wimplicit- int]
 main ()^~~~ 

edit: I have just realized that the code did, in fact, compile. I just didn't realize it had overwritten the file for another program I compiled. I've edited the question itself, as I think that has more value. (Originally I asked why this program was not compiling)

DirtyDan77
  • 13
  • 4
  • 1
    That’s not an error, it’s a warning. – Jeremy Friesner Dec 30 '19 at 03:02
  • The `C` language has evolved considerably since 1988 (when the book you are using was published). – Adrian Mole Dec 30 '19 at 03:03
  • 1
    [What should main() return in C and C++?](http://stackoverflow.com/questions/204476/) and [C11 Standard - §5.1.2.2.1 Program startup(p1)](http://port70.net/~nsz/c/c11/n1570.html#5.1.2.2.1p1). But note In a freestanding environment (in which C program execution may take place without any benefit of an operating system), the name and type of the function called at program startup are implementation-defined. See: [C11 Standard - 5.1.2.1 Freestanding environment](http://port70.net/~nsz/c/c11/n1570.html#5.1.2.1) – David C. Rankin Dec 30 '19 at 03:08
  • With the edit, this is a duplicate of [this question](https://stackoverflow.com/questions/12373538/warning-return-type-defaults-to-int-wreturn-type). – jirassimok Dec 30 '19 at 03:09
  • The rules for standard C have changed since 1988 when K&R 2nd Edn was published — what was acceptable in the first version of the standard (C89/C90) is not officially acceptable in C99, let alone C11 or C18. This is, sadly, an example of why K&R is no longer the best book for beginners — it doesn't describe the current version of C. Some of the examples later in the book run into problems; the implementation of `readdir()` etc no longer works, and POSIX has usurped [`getline()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/getline.html) which conflicts with the book's versions. – Jonathan Leffler Dec 30 '19 at 04:01
  • @JonathanLeffler Out of curiosity, what book would you reccomend for a new programmer? After taking your advice, I found a copy of 'C programming: A Modern Approach'. Published in 2008. In your opinion, would this be a good starting point, given recent revisions to C such as C11 and C18? – DirtyDan77 Dec 30 '19 at 08:50

4 Answers4

1

You haven't included a return type for main. You probably want to return 0 at the end of it, as that is the standard exit code for no error. Revised your code would be

#include <stdio.h>

int main() {
    printf("hello, world\n");
    return 0;
}
kopecs
  • 1,545
  • 10
  • 20
0

Try to:

Use int main() since int is return type of main.

EDIT:

In C89, the default return type is int. This default was removed in C99 and compilers are helpful reminding you that your C-style with no int before main() is out of date.

Azevinho
  • 23
  • 1
  • 4
0

That is not an error, it's just a warning. Your code does compile, trying running the resulting binary with ./a.out

GCC is just warning you that you didn't specify a return type for main, and that it will automatically return an integer.

nickelpro
  • 2,537
  • 1
  • 19
  • 25
0

Your main signauture is wrong. You should consider reading this answer to get a C11 standardized signature of the main function : https://stackoverflow.com/a/2108208/8141369