Can the function getline() from stdio.h coexist with the one in K&R88?

127 Views Asked by At

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.
1

There are 1 best solutions below

0
P.P On

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.