9

I have the following c code:

#include<stdio.h>

int main(void)
{
    char buff[10];
    memset(buff,0,sizeof(buff));

    gets(buff);

    printf("\n The buffer entered is [%s]\n",buff);

    return 0;
}

When I run the code, I get the following warning:

warning: implicit declaration of function ‘memset’ [-Wimplicit-function-declaration]

How should I solve the problem?

Thanks

Michael
  • 121
  • 1
  • 1
  • 4

2 Answers2

24

Add

#include <string.h>

at the top of file.

This because is the header file where the memset prototype can be found by compiler.

Avoid using gets function... Use scanf or fgets instead.

Take a look HERE

Community
  • 1
  • 1
LPs
  • 16,045
  • 8
  • 30
  • 61
  • now I have this error: warning: implicit declaration of function ‘gets’ [-Wimplicit-function-declaration] gets(buff); – Michael Jan 20 '16 at 10:48
  • @Michael: see [this answer](http://stackoverflow.com/a/2506036/253056) to learn how to resolve these kinds of issues quickly and easily. – Paul R Jan 20 '16 at 10:49
  • 1
    @Michael Did you remove stdio include? BTW avoid to use that function... Use [scanf](http://www.tutorialspoint.com/c_standard_library/c_function_scanf.htm) or [fgets](http://www.tutorialspoint.com/c_standard_library/c_function_fgets.htm) instead. Take a look [HERE](http://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) – LPs Jan 20 '16 at 10:50
2

Add

#include <string.h>

memset is available in string.h

Gopi
  • 19,784
  • 4
  • 24
  • 36
  • now I have this error: warning: implicit declaration of function ‘gets’ [-Wimplicit-function-declaration] gets(buff); – Michael Jan 20 '16 at 10:49