×

gets() function in C

gets() is a pre-define or built-in function present in the stdio.h header file. stdio stands for standard input and output. gets() function is used to read a stream and store it in a C string. gets() function read the character until a newline character is entered or enter key is entered, or the end of the file is reached.

If we give the string size 15 and the input character are greater than 20, The gets() function will read 20 characters without checking the maximum limit, but while compiling, we get buffer overflow error.

gets()  function also reads spaces. The array bound is not checked by the gets() function.

gets() function takes a string as the parameter and returns the string if there are no errors. Else, return a null value. No character given in input at the end of file.

Syntax to call gets() function:

gets(input string name);

Example: gets(string1);

Example  program on gets() function:

// program to read and print a string using gets() function.
#include <stdio.h>
int main ()  // main function
{
   char s[50]; // string declaration with size 50.
   printf("Enter a string : ");
   // Reading the string
   gets(s); // function call
   printf("  \nThe string which you have entered : %s", s);
   return(0);
}

Output:

Enter a string : Hello, Welcome to JavaTpoint tutorials and Examples
The string which you have entered : Hello, Welcome to JavaTpoint tutorials and Examples

Working of gets():

If we want to use gets() function first,  include the <stdio.h> header file. Now the program body starts first we declared a string s of size 50.

Next, we gave a printf statement. In the next step, we call the gets function, and it reads the input string till a new line character is entered.

Now the reading part is done, we are printing the input string on the console output screen.

Advantages of gets() function:

  1. gets() function reads multiple characters at a time.
  2. Gets() function accepts spaces in the string.
  3. It takes a single string as a parameter. Ex: gets(str);

Disadvantages of gets() function:

  1. The gets() function reads the input characters until the new line character key is entered.
  2. If the input string exceeds the size of the string we get the buffer overflow error.
  3. We can’t check the bounds of the input array.
  4. gets() function  is not safe.
  5. Size of the string is fixed.

Simple example program to show buffer overflow error message:

// program to show buffer overflow error message.
#include <stdio.h>
int main ()  // main function
{
   char s[10]; // string declaration with size 10
   printf("Enter a string : ");
   // Reading the string
   gets(s); // function call
   printf("  \nThe string which you have entered : %s", s);
   return(0);
}

Output:

Enter a string : Welcome to JavaTpoint
*** stack smashing detected ***: terminated

Aborted

Here the size of the given string is greaterthen the given string. The gets() function reads the input successfully, but while returning, it displays *** stack smashing detected ***: terminated Aborted.

Example program to take input string using gets() function and print the length of the string.

// program to calculate the length of the given string using iteration method.
#include <stdio.h>
#include <string.h>
#define max 100
int main ()  // main function
{
   char s[max]; // string declaration.
   int i, c=0; // initially length of the string c=0
   printf("Enter a string : ");
    // Reading the string
   gets(s); // function call
   printf("  \nThe string you have entered : %s", s);
   for(i=0;s[i]!='\0';i++)
   {
       c++; // incrementing the length of the string for each iteration
   }
   // printing the length of the string.
   printf(" \nThe length of the given string is= %d", c);
   return(0);
}

Output:

Enter a string : Hello, Welcome to JavaTpoint tutorials and examples
The string you have entered : Hello, Welcome to JavaTpoint tutorials and examples 
The length of the given string is= 51

Example program to read the string using gets() function and display the size of the string using strlen() function.

// program to calculate the length of the given string.
#include <stdio.h>
#include <string.h>
#define max 100
int main ()  // main function
{
   char s[max]; // string declaration.
   int a;
   printf("Enter a string : ");
    // Reading the string
   gets(s); // function call
   printf("  \nThe string you have entered : %s", s);
   a = strlen(s); // calculating the string length using strlen() function
   // printing the length of the string.
   printf(" \nThe length of the given string is= %d", a);
   return(0);
}

Output:   

Enter a string : Hello, Welcome to JavaTpoint tutorials and examples
The string you have entered : Hello, Welcome to JavaTpoint tutorials and examples 
The length of the given string is= 51

Conclusion:

In this article, we learned how to use the gets() function, its uses, and the advantages and disadvantages of the gets() function.

Examples of the gets() function are also attached to this article. With the help of examples, we can learn the implementation of gets() function using c, and we can identify in which case we can implement this function.


Related Topics

Exit control loop in C

To examine the termination condition for an exit, we usually use an exit control loop in C. If the evaluation for termination condition results in true, then the control would...

3 minutes read.

How to return a Pointer from a Function in C?

In the C programming language, pointers are variables that hold the address memory location of another variable. We could also input pointers to a function and return pointers from a...

3 minutes read.

Stack implementation in C

Stack implementation in C Stack stores the data in a particular order. It is a linear data structure that follows the principle of the Last In First Out (LIFO) technique where...

3 minutes read.

Modulus on Negative Numbers in C

In this tutorial, we will explore some examples of modulus on negative number. What is Modulus of Negative number? By omitting the minus sign, one can determine the modulus of a negative...

3 minutes read.

Bitwise XOR Operator in C

What is Bitwise? As we know, bits are the smallest unit of a number and are mostly used in a computing system. So, in bitwise, we do operations on bits instead...

3 minutes read.

C printf and Scanf

Input-Output functions in C Programming In C Language, the printf() and scanf() are inbuilt library functions that used for input and output. It is defined in the header file“<stdio.h>”. printf() Function: In C...

2 minutes read.

C Switch Statements

Switch-case statement comes under the Selection control statement; there are four types of Control statements Decision-making statements (if, if-else)Selection statements (Switch-case)Iteration statements (for, while, do-while)Jump statements (break, continue, goto) If we want...

4 minutes read.

Factorial Program in C Using While Loop

Before looking into the mechanism of factorial program, first, you have to understand about the working of a while loop. While Loop The syntax that has been used for the “while” loop...

4 minutes read.

10 Best IDEs for C or C++ Developers in 2024

Nobody can deny the fact that C and C++ were the first programming languages used by significant developers worldwide. Even now, newcomers who want to start programming are most frequently...

6 minutes read.

Command line arguments in C

Command line arguments in C The arguments that are generally passed from the line of command are referred to as command line arguments. These command line arguments are always handled by...

3 minutes read.

Pi() Function in C

Pi: Mathematic constants are not defined in the c or c++ programming languages. To use the Mathematical constants firstly, we have to define the _USE_MATH_DEFINES and then declare the cmath and...

3 minutes read.

Bigint (BIG INTEGERS) in C with Example

The maximum number of digits that a long, long int can have in C/C++ is 20. The issue is how to store the 22-digit number, which is difficult to do...

9 minutes read.

Fibonacci series program in C using Recursion

In this tutorial, we’ll explore how to utilise recursion to build the Fibonacci sequence in C language. What does the term "Fibonacci Series" mean? The following number in a Fibonacci number series is...

2 minutes read.

Strcpy() in C

In C language many operations can be performed on a string. Characters in a sequence are known as string. Note:   To use this function we must include the #include<string.h> header file...

4 minutes read.

Merge Sort in C

The merge sort follows the principle of the divide and conquers algorithm. Firstly it divides the input of an array into two halves and then calls itself for the two...

4 minutes read.

Flexible Array Members in a Structure in C

There is flexibility to declare array from c99 generation onwards that declaration of the collections can be made possible without even mentioning its dimension, which means that the displays are...

4 minutes read.

memcmp() in C

Introduction: In this article we are discuss about memcmp() function in C. This function permits the person to evaluate the bytes of the two characters, as mentioned above. Depending on...

4 minutes read.

Multithreading in C interview questions

Q1. What exactly is a thread? Ans: A thread is a brief sequence of instructions intended to be planned and carried out by the CPU apart from the parent process. Q2. Can...

4 minutes read.

Comments in C

Comments are used to comment on the line of code in the program. Comments are a way of inserting remarks and reminders into code without affecting its behavior. The compiler...

1 minute read.

How to convert a number to words in C

There are many ways available for converting an integer or a number to its word form. For example, consider a number as 12345. This number can be represented in two...

3 minutes read.