gets() Function in C

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

The gets() function is used to read characters from stdin and store them into the provided character array until a new line or EOF, end of the file is reached. It is declared in <stdio.h> header file.

Build an AI-First Career, Master the Complete Skillset

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program
NSDC Certified

AI Forward Deployed Engineer Program

Full-stack engineering, production AI and client-facing consulting

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program

Syntax of gets() Function in C

The syntax of gets function in C is as follows:

Parameters of gets() Function in C

Parameter: Pointer of char data type.

It accepts a pointer to the array of characters and after the read operation, the data is stored in that array.

Return value of gets() Function in C

Return Value: Pointer of char data type.

  • Success: It returns the pointer to the same string/(array of characters ) where data was stored.
  • Failure: Null Pointer.
Sharpen Your Fundamentals with Free Learning

Example

Input:

Enter a String: Hello World!

Output:

In the above code, we declare a character array str then take input for that using gets(). In the end, we print the value of str.

How Scaler Transformed Careers in Different Fields

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

What is gets() Function in C?

The gets function in C reads characters from the stdin until a new line or end of the file is reached. It accepts a pointer to the memory where it stores those array of characters. A terminating null character(\0) is automatically appended after the successful writing of characters to the string.

This function was removed in C 11 due to its buffer overflow problem. While using the gets function in C, compiler shows warnings like this,

  1. warning: ‘gets’ is deprecated
  2. warning: the 'gets' function is dangerous and should not be used

Limitation of gets() Function in C

The function does not prevent buffer overflow at destination memory, which means the function does not have a limit check to prevent reading after the characters exceed the size of the array passed to the function. The program will terminate with a runtime error in such a scenario.

A function with the bound check was introduced in C 11 that has been named gets_s(). It is similar to gets but it accepts a size/limit and reads the characters until the size is not exceeded. By providing the limit, it fixes the problem of buffer overflows associated with the gets function. Below is the syntax of gets_s() function.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Difference between scanf() and gets() Function

scanf() gets()
scanf() function takes input from stdin according to the format specifiers. By default, it considers whitespace, newline, and tab as delimiters.gets() function takes input from stdin until a new line (\n) character or EOF is reached.
It doesn't consider whitespace as input, although we can use a custom [^\n] delimiter to read the entire line.It reads the entire line of string by default.
int scanf(const char *format, Object *arg(s));char* gets ( char* str );
It can accept the input in any data type according to the format specifier.It accepts only the string data type.

More Examples

Buffer Overflow Example

Input:

Enter a String: This string is going to exceed the limit of destination array.

Output:

As we can see the program throws a runtime error as the size of input string exceeds the size of the character array causing a buffer overflow.

Conclusion

  • We use the gets function in C to take the input of the entire line into a string.
  • gets() take a pointer to the character array and store the input string into it. After this operation, it returns the same pointer.
  • This function was removed in C 11 because of some limitations.
  • The alternative to gets function was introduced as gets_s function which accepts a limit to prevent buffer overflow.
  • The most suitable alternative to gets function is fgets function.

See Also

  • scanf()
  • fgets()
  • gets_s()