Easy Tutorial
❮ Perl Output Chinese Android Tutorial Handler Message ❯

Using gets() in C prompts a warning: this program uses gets(), which is unsafe.

Category Programming Techniques

A warning will appear when using gets() in C during compilation:

warning: this program uses gets(), which is unsafe.

gets() is unsafe because you provide it with a buffer, but you do not tell it the size of this buffer, nor do you know how much the input content is. The input content may exceed the end of the buffer, causing your program to crash.

The solution is to use fgets as a replacement:

char buffer[bufsize];
fgets(buffer, bufsize, stdin);

Example:

// Using gets()
char buffer[4096];
gets(buffer);

// Replacing gets() with fgets()
char buffer[4096];
fgets(buffer, (sizeof buffer / sizeof buffer[0]), stdin);

** Click here to share notes

Cancel

-

-

-

❮ Perl Output Chinese Android Tutorial Handler Message ❯