18

I understand that an 'implicit declaration' usually means that the function must be placed at the top of the program before calling it or that I need to declare the prototype.
However, gets should be in the stdio.h files (which I have included).
Is there any way to fix this?

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
   char ch, file_name[25];
   FILE *fp;

   printf("Enter the name of file you wish to see\n");
   gets(file_name);
   fp = fopen(file_name,"r"); // read mode
   if( fp == NULL )
   {
      perror("Error while opening the file.\n");
      exit(EXIT_FAILURE);
   }
}
zx485
  • 28,498
  • 28
  • 50
  • 59
Death_by_Ch0colate
  • 379
  • 1
  • 2
  • 7

2 Answers2

27

You are right that if you include proper headers, you shouldn't get the implicit declaration warning.

However, the function gets() has been removed from C11 standard. That means there's no longer a prototype for gets() in <stdio.h>. gets() used to be in <stdio.h>.

The reason for the removal of gets() is quite well known: It can't protect against the buffer overrun. As such, you should never use gets() and use fgets() instead and take care of the trailing newline, if any.

P.P
  • 117,907
  • 20
  • 175
  • 238
  • 1
    (If you are inetersted) The weird part is, even though it gives the error it is still able to run just fine. – Death_by_Ch0colate Dec 01 '15 at 22:58
  • 2
    That's probably because the libraries still have the function, possibly to avoid breaking ancient code. But no new code should ever use `gets()`. – P.P Dec 01 '15 at 22:59
17

gets() was removed from the C11 standard. Do not use it. Here is a simple alternative:

#include <stdio.h>
#include <string.h>

char buf[1024];  // or whatever size fits your needs.

if (fgets(buf, sizeof buf, stdin)) {
    buf[strcspn(buf, "\n")] = '\0';
    // handle the input as you would have from gets
} else {
    // handle end of file
}

You can wrap this code in a function and use that as a replacement for gets:

char *mygets(char *buf, size_t size) {
    if (buf != NULL && size > 0) {
        if (fgets(buf, size, stdin)) {
            buf[strcspn(buf, "\n")] = '\0';
            return buf;
        }
        *buf = '\0';  /* clear buffer at end of file */
    }
    return NULL;
}

And use it in your code:

int main(void) {
    char file_name[25];
    FILE *fp;

    printf("Enter the name of file you wish to see\n");
    mygets(file_name, sizeof file_name);
    fp = fopen(file_name, "r"); // read mode
    if (fp == NULL) {
        perror("Error while opening the file.\n");
        exit(EXIT_FAILURE);
    }
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • if you use gets include but like everyone said you're putting your program for a buffer overflow and exploitation. Use it only if you're testing an exploit case with this function. – Samir Ouldsaadi Apr 26 '18 at 11:03
  • @chqrlie Isn't `sizeof buf` or `sizeof file_name` missing parenthesis ? And why do you need the size has an external parameter of `mygets` instead of doing `size=sizeof(buf)` inside your `mygets` function ? – SebMa May 13 '20 at 10:22
  • @SebMa: parentheses are only required when the argument of `sizeof` is a type. For an expression, `sizeof` behaves as a prefix operator. I only use optional parentheses when I use `sizeof` in an expression such as `sizeof(*buf) * 10` but even in this expression, the parentheses are optional and redundant. – chqrlie May 13 '20 at 12:47
  • @SebMa: regarding the `size` argument for `mygets`, it is required because the size of the array pinted to by `buf` cannot be determined from the pointer itself. `sizeof(buf)` is just the size of the pointer, not that of the array. – chqrlie May 13 '20 at 12:49