0

I am trying to use getline() function. I am using correct syntax I guess and also I have included #include<stdio.h> header file also. Still it is showing that [Error] 'getline' was not declared in this scope

here is my code

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
    char *str;
    int bytes_read;
    int size=10;
    printf("please enter a string");
    str=(char *)malloc(size);
    bytes_read=getline(&str,&size,stdin);
    puts(str);
}
  • You appear to be using Windows. On linux you have to `#define _POSIX_C_SOURCE 20809L` before you include stdio.h. – Allan Wind Jan 01 '23 at 06:20
  • 2
    Borland’s Turbo C follows an _ancient_ standard. Even modern versions do not have POSIX functions like `getline()`. (I think.) – Dúthomhas Jan 01 '23 at 06:20
  • To those who wonders, the OP seem to be using [this `getline` function](https://pubs.opengroup.org/onlinepubs/9699919799/functions/getdelim.html). – Some programmer dude Jan 01 '23 at 06:21
  • [Same, but at cppreference.com](https://en.cppreference.com/w/c/experimental/dynamic/getline) – Dúthomhas Jan 01 '23 at 06:23
  • As mentioned by @AllanWind you seem to be using Windows. Otherwise you would not include the non-standard and originally DOS header file ``. As pointed out by my link above the `getline` function is a POSIX function, available in Unix or Unix-like systems like macOS or Linux. – Some programmer dude Jan 01 '23 at 06:23
  • [Someone’s implementation of the functions on github.](https://gist.github.com/ingramj/1105106/adfad1a7b1f0575dd7737c296d644856f2c2d074) – Dúthomhas Jan 01 '23 at 06:25
  • @AllanWind yes I am using windows. So is it like I can't use getline function on windows – Burhan Aijaz Jan 01 '23 at 06:30
  • @Someprogrammerdude So how can I use getline on windows and I am a new programmer so I don't know much, so please guide me – Burhan Aijaz Jan 01 '23 at 06:32
  • If you use the link from @Dúthomhas, then line 104 is better as `if ((size_t)bytes >= *n - 1) {` to avoid the warning for signed/unsigned comparison. – David C. Rankin Jan 01 '23 at 06:34
  • 1
    Unless you use a Unix-like environment (like e.g. [Cygwin](https://www.cygwin.com), [Windows Subsystem for Linux](https://learn.microsoft.com/en-us/windows/wsl/install), Linux in a virtual machine, or perhaps [MSYS2](https://www.msys2.org)) or download a library which have it, you can't. – Some programmer dude Jan 01 '23 at 06:35
  • 1
    I'll second MSYS2 -- it is a phenomenal build environment for windows that provides (for all practical purposes) a Linux terminal running natively in windows without the overhead of a full virtualized OS. It has the latest gcc and latest of all Linux utilities and uses Archlinux's `pacman` package manager to keep everything up to date. – David C. Rankin Jan 01 '23 at 06:40
  • 1
    `int size` needs to be `size_t size`. Since you're passing its address to `getline`, it must have the correct size. – Tom Karzes Jan 01 '23 at 06:43
  • 1
    On another note, `getline` will allocate memory itself, to fit the whole line. Initialize `str` to `NULL` and `size` to `0`, and `getline` will handle it automatically for you. Also, in C you [shouldn't cast the result of `malloc`](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc/605858) (or any function returning `void *`). – Some programmer dude Jan 01 '23 at 06:51
  • Burhan Aijaz, what compiler are you using? – chux - Reinstate Monica Jan 01 '23 at 07:13
  • Any particular reason to dynamically allocate a string of a trivial size known at compile time? – Chris Jan 01 '23 at 07:55

1 Answers1

0

getline is a POSIX function, and Windows isn't POSIX, so it doesn't have some POSIX C functions available.

You'll need to define the feature macro _GNU_SOURCE in order to make it available for your code.

#define _GNU_SOURCE