Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024
Difficulty: Easy

gets in C++

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

Welcome fellow coders! Today, we're going to explore an intriguing function in C++ programming: gets(). It's a simple, yet powerful function to read input from the user. 

gets in C++

However, it's critical to understand its usage and potential pitfalls.

What is gets()?

The gets() function is a standard library function in C++ that reads a line from stdin and stores it into the string pointed to by a char pointer.

Basic Syntax

The basic syntax of the gets() function is as follows:

char *gets(char *s);

Here, s is a pointer to an array where the string read is stored.

Simple Example

#include <stdio.h>

int main() {
   char str[50];


   printf("Enter a string : ");
   gets(str);

   printf("\nYou entered: ");
   puts(str);


   return(0);
}

In this example, the user can enter a string, and gets() reads the string and stores it in the str array.

Output

Output
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

The Risk of gets()

While gets() seems straightforward and useful, it's known for being risky because it doesn't check the array bounds. This means if the input has more characters than the array can hold, gets() will continue to store them, causing a buffer overflow. This can lead to errors, crashes, and security vulnerabilities.

Safer Alternatives to gets()

Due to the risks associated with gets(), it's generally recommended to use safer alternatives like fgets(). Here's an example:

#include <stdio.h>


int main() {
   char str[50];


   printf("Enter a string : ");
   fgets(str, 50, stdin);

   printf("\nYou entered: ");
   puts(str);


   return(0);
}

Output

Outptu

In this code, fgets() reads a string from the user but stops after 49 characters (leaving space for the null terminator), preventing a potential buffer overflow.

Also read -  File Handling in CPP

Frequently Asked Questions

What does the gets() function do in C++?

gets() reads a line from stdin and stores it into the string pointed to by a char pointer.

Why is gets() considered dangerous in C++?

gets() is considered dangerous because it doesn't check the array bounds, which can lead to buffer overflow if the input is longer than the array.

What's a safer alternative to gets() in C++?

A safer alternative to gets() is fgets(), which lets you specify the maximum number of characters to be read, preventing buffer overflow.

Conclusion

Though gets() is simple and easy to use, it's critical to understand the associated risks. It's generally recommended to use safer alternatives like fgets(). The key to becoming a proficient coder is to write code that's not only functional but also secure. 
Recommended articles:


We hope this blog helped you to understand the concept of the Member Function in C++. You can refer to our guided paths on the Coding Ninjas Studio platform. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

To practice and improve yourself in the interview, you can also check out Top 100 SQL problemsInterview experienceCoding interview questions, and the Ultimate guide path for interviews.

Happy Learning!! 

Topics covered
1.
Introduction
2.
What is gets()?
2.1.
Basic Syntax
3.
The Risk of gets()
3.1.
Safer Alternatives to gets()
4.
Frequently Asked Questions
4.1.
What does the gets() function do in C++?
4.2.
Why is gets() considered dangerous in C++?
4.3.
What's a safer alternative to gets() in C++?
5.
Conclusion