2

I'm knee-deep in [K&R88] and I get chided by gcc because the function getline(), which K&R use as example and practise material, is now in stdio.h (and has been since around 2010, I'm told.)

Would there be any manner to tell the compiler to play it like it's 1988 and to have my homespun version supersede the one from the library ?

(yes it's futile, but wadding in [K&R88] is my new hobby ;-)

Obligatory compiler output :

gcc -g -Wall -o "pgm" "pgm.c" (in directory: /home/eric/Development/6.087)
pgm.c:9:7: error: conflicting types for ‘getline’
    9 | char *getline(){
      |       ^~~~~~~
In file included from pgm.c:1:
/usr/include/stdio.h:616:18: note: previous declaration of ‘getline’ was here
  616 | extern __ssize_t getline (char **__restrict __lineptr,
      |                  ^~~~~~~
Compilation failed.
Éric Viala
  • 596
  • 2
  • 12
  • 2
    `getline()` is not described by the [C Standard](http://port70.net/~nsz/c/c11/n1570.html). Your compiler is being helpful and providing a larger library than strictly required. To prevent it from doing that, try `gcc -std=c11 -pedantic -Wall -Wextra ...` (the absence of `std=XXX` is currently equivalent to `std=gnu17` and gnu17 is Standard C with GNU extras) – pmg Feb 16 '22 at 18:33
  • 1
    For this era of programming, maybe `-std=c11` should be changed to `-std=c90` or even `-ansi` (which also enables trigraph sequences). – Ian Abbott Feb 16 '22 at 18:40

1 Answers1

5

The getline function that conflicts with yours isn't part of standard C - it's POSIX's getline.

If you compile with standard C using -std:

gcc -g -Wall -std=c17 -o pgm pgm.c

you can avoid the conflict.

Having said that there's no reason to learn from K&R C (it's more of a reference imo) if you're starting out in C. And we're not going back to 1988 either ;-)

The language has changed a lot since and there's a good list of recommendations available at The Definitive C Book Guide and List.

P.P
  • 117,907
  • 20
  • 175
  • 238