gets() puts() string functions use and misuse
Using gets() and puts() string function and its serious limitations
Learning Objectives
- Learning to use gets() and puts() functions defined in stdio.h header.
- Learning why these functions are not good in modern applications.
Source Code
|
|
Run Output
Code Understanding
#include <stdio.h>
This header file need to be included for using gets() and puts() functions.
char entry1[11]; char entry2[11];
Two character array for string collection have been defined
cout<<“Enter some text (max 10 char.): “; //Prompt for entry, warning for size necessary
gets(entry1); //Collects entry upto the size given and can collect even more characters and write it over other adjacent memory locations.
cout<<“Enter some text again (max 10 char.): “; //Prompt for entry again.
gets(entry2); //Collects another entry and if user’s entry is more than given size it can even corrupt the first entry.
puts(entry1); puts(entry2); //Both entries are displayed.
Notes
- The function gets() in particular is not good for the limitations explained above. But it continues to be present even in new libraries for compatibility sake.
- The modern library object cin and cin.getline getline(cin,<str>) etc. are good replacements and the preferred ones in the c++ use.
Suggested Filename(s): getsputs.cpp
Created: 13-Jan-2018 18:59 | Updated: 13-Jan-2018 19:17|
