gets() puts() string functions use and misuse – CSKC
Sign In

Search

Code Learning #CPP#2606    siteicon   siteicon  

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

TC++ #2606

 

Source Code

×

Ask AI About This Code

Default Prompt that is passed along with the code is given below, You can write your own prompt if you so desire.



×

Test CPP

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



Share

Created: 13-Jan-2018 18:59 | Updated: 13-Jan-2018 19:17|