answersLogoWhite

0


Best Answer

It is unsafe. In order to use gets() safely, you need to know how many characters you will be reading to ensure your character buffer is large enough:

char buffer[10];

while (gets (buffer) != 0) { ...process buffer... }

The above code has undefined behaviour when the number of characters read is 10 or more (you need one character for the null-terminator). This is because the character buffer, str, decays to a pointer (referencing &str[0]) and the function, gets(), cannot determine the number of characters in a buffer by its pointer alone.

The gets() function was dropped from the C standard in 2011, however some implementations still include it. To avoid the warning, use the fgets() function instead. This allows you to specify the length of your buffer and (when used correctly) prevents buffer overflow.

char buffer[10];

while (fgets (buffer, 10, stdin) != 0) { ...process buffer... }

User Avatar

Wiki User

6y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why gets function gives a warning every time you compile a c program?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What function does every C program have Why should you split large programs into several functions?

Every C program has a main() function.


What must every c program have?

A main function must be present in every C program.


What is the name of the function which must be defined in a Java program?

The main function. Every program must have a main function and it must be declared static.


Why main function c contained?

1..Every program start with main(). 2. Compiler firstly compile line main(). 3. At least one fuction in C language. 4. Syntax of function returntype functionname(parameter/arguments) { body } int main(void) { printf("Hello"); } Sachin bhardwaj skbmca@gmail.com 9868547722


How do you write mainframe program?

Nothing special. Of course before you start, you should know how to compile, link, run and debug your programs, but this goes for every platform.


What is the only function all C plus plus programs must contain?

Every C plus plus program that is a main program must have the function 'main'.


What is the meaning of statement missing in function main in c language?

Every C program must have a function, named main(), which is where the program starts execution. If there is no function main(), the computer does not know where to start running the program. The function main() must also do something; if it is just empty, some smarter compilers will note that there is nothing for the program to do, and will give this sort of error message to indicate that you forgot to tell the program what to do.


How do you write a programm in c plus plus without using function main?

I don't think its possible. Every C++ program must at least have the main function.


Why every program runs with its main class name in Java?

The "main" function name is reserved and specifies the entry point of the application.


What are the advantages and disadvantages of becoming a interpreter?

* Interpreters are useful for program development when execution speed is not important. As the interpreter is in command of the execution process debugging features can be build in. * Debugging is easier since the interpreter stops when it encounters an error. If an error is deducted there is no need to re translate the whole program, * There is no lengthy "compile time", i.e. you do not have to wait between writing a program and running it, for it to compile. As soon as you have written a program, you can run it. * Interpreters normally translate and execute programs line by line, converting each program statement into a sequence of machine code instructions and executing these instructions without retaining the translated version. i.e. In a program with a loop, that the same statement will be translated every time it is encounted. Therefore Interpreter programs are usually slower in execution than compiled programs. * No object code is produced, so a translation has to be done every time the program is running. Source code is required for the program to be executed


Define multi function in c plus plus?

Multi-function simply means more than one function. Every program must have at least one function, the main function (the entry point of the application), and although you can write an entire program using just this one function, breaking the program down into several smaller functions makes the code easier to read and maintain. A major advantage of multi-function programming is that functions can be called as often as required -- there is no need to write the same piece of code over and over again.


How will you overload a template function?

The only reason to overload a template function is to provide an overload that differs in the number and type of arguments that cannot be addressed by the template function alone. You simply declare them as you would any other overload. The overloads may themselves be template functions, but there must be no ambiguity: every template function must generate an unique function signature. Remember that template functions generate overloads at compile time on an as-required basis.