0

I recently picked up K&R the second edition to learn C. However on the first program, a simple hello world. It is giving me this compile issue

hello.c:3:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
main()
^
1 warning generated.

My code is

#include <stdio.h>
main()
{
  printf("Hello world\n");
} 

and I'm compiling it by going into the folder where this program is located and using gcc hello.c -o hello

Qman485
  • 115
  • 2
  • 9
  • 1
    use `-std=gnu89` compiler switch – M.M May 12 '16 at 02:55
  • @M.M After I exit the terminal, does the compiler switch back to the old one? – Qman485 May 12 '16 at 03:00
  • 1
    It uses whatever you type each time. You can use a makefile, shell script or shell alias to avoid having to retype common switches – M.M May 12 '16 at 03:03
  • Don't use that book to learn C anymore! It does not teach standard C, not even the major overhaul C99. Use a modern book with **at least** C99 - better the current version C11. – too honest for this site May 12 '16 at 15:19
  • @M.M: That is wrong advice. Better not to use C90 at all. Note that such declarators are explicitly listed as obsolescent features according to the standard and the compiler is required to warn for good reasons since C99. – too honest for this site May 12 '16 at 15:21
  • @Olaf plenty of instructors still use K&R to teach the language. It still has a very nice and clean approach to teaching the language, and a 'conversational' tone that make it a fun book to look through. I already know C, but I'm taking a Purdue class in it to get a degree & my instructor is using K&R. – Wyatt Ward Jan 03 '18 at 03:14
  • @Wyatt8740 "It still has a very nice and clean approach to teaching the language" - The problem is, it does **not** cover the C language since 1999 ()if we talk about the 2nd and last revision). C has evolveed a lot with C99 and more with C11. Whereas C11 mostly added new types and constructs, no/less behaviour has changed compared to C99 va. C90 (most, if not all changes in the standard to these were clarifications and describe common practice). So no, it is **not** a good book to learn the C language!. – too honest for this site Jan 03 '18 at 09:57
  • @Wyatt8740 The code in the question is **one** example for the changes not covered by K&R. – too honest for this site Jan 03 '18 at 09:59

1 Answers1

3

Ah yes, this is old. Declaring functions or variables without their types is going away.

#include <stdio.h>
int main()
{
  printf("Hello world\n");
} 
Joshua
  • 40,822
  • 8
  • 72
  • 132
  • 2
    `int main(void)` looks better. – MikeCAT May 12 '16 at 02:23
  • 1
    @MikeCAT: `int main(void)` **is** better. – Keith Thompson May 12 '16 at 02:24
  • 1
    You really don't want to write int main(void) because main is declared as int main(int argc, char **argv); (which he will get to later) It will probably never bite but ... – Joshua May 12 '16 at 02:25
  • @Joshua ahh it worked! Thanks! Is it not worth using this book if everything is dated? – Qman485 May 12 '16 at 02:29
  • The book is still on my shelf. There's really only a handful of dated things to worry about. The only other one I recall is external symbols aren't restricted to 6 characters anymore. – Joshua May 12 '16 at 02:32
  • @Joshua awesome! I will keep learning then, cheers! – Qman485 May 12 '16 at 02:33
  • 4
    @Joshua `int main(void)` and `int main(int argc, char **argv);` are both fully valid signatures for `main`. There is some debate over whether `int main()` is valid; strict reading of the C Standard suggests it isn't, but some argue that it should be valid and the standard is unclear or faulty. – M.M May 12 '16 at 02:56