6

I have just begun my programming journey. I code in the Ubuntu terminal. I am facing a problem while compiling a program where the gets() function is used.

#include<stdio.h>

/*example of multi char i/p function*/
void main()
{
    char loki[10];
    gets(loki);
    printf("puts(loki)");
}

The error I am getting is:

warning: 'implicit declaration of function ‘gets’; did you mean ‘fgets’? [-Wimplicit-function-declaration]
Acorn
  • 24,970
  • 5
  • 40
  • 69
Lokesh Jain
  • 71
  • 1
  • 1
  • 5

2 Answers2

11

gets was removed in C11, because it is impossible to use correctly. gets does not know how many characters it can store into the array and continues to write as many as the user provides, which leads to the program to have undefined behaviour - crashes, modification of unrelated data etc.

The fix is to use fgets instead, though keeping in mind that it leaves a newline in the buffer:

#include <stdio.h>

// example of multi char i/p function
int main(void)
{
    char loki[10];
    fgets(loki, 10, stdin);

    // now loki will have the new line as the last character
    // if less than 9 characters were on the line

    // we can remove the extra with `strcspn`:
    loki[strcspn(loki, "\n")] = 0;

    // this will print the given string followed by an extra newline.
    puts(loki);
}
8

On Ubuntu, run man gets in a terminal. It should show you this gets(3) man page.

That documentation states, in written English:

Never use this function.

More generally, before programming, read documentation. For C on Linux, consider reading the man pages, this C reference, many C programming tutorials, and the C11 standard n1570.

English wikipedia also mentions gets

At last,

warning: 'implicit declaration of function ‘gets’; did you mean ‘fgets’? [-Wimplicit-function-declaration]

Seems quite clear to me, since written in English. As a rule of thumb, ensure that your program compiles without warnings. Read also How to debug small programs.

You could be interested in understanding the acronyms RTFM and STFW.

I learned C and Unix programming (in 1985) by reading SunOS3 man pages (at that time on paper, at work, sold with the Sun3/160 workstation I had the privilege to use then), from section 1 to section 9.

You could read Advanced Linux Programming before the man pages.


I have just begun my programming journey.

Then I recommend reading SICP. In my grand-father's eyes, it is still the best introduction to programming, even in 2019. See also these hints. It does not matter if SICP is using some programming language which is not very used in professional real life (but look into Guile): programming is about concepts, not about coding. The concepts you will learn with SICP surely will help you to write better C code later. Of course read http://norvig.com/21-days.html


NB. I am French (born in 1959) so not a native English speaker. But I was taught to read, including during my PhD studies, and of course during high school and by my own parents. And when I taught at university some CS stuff, the first thing I told to students is read. Never be ashamed of reading.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547