×

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

C Program to Find Largest Number Using Dynamic Memory Allocation

This tutorial will teach us how to locate the highest number a user has ever input into a dynamically allocated memory. C Program: #include <stdio.h> #include <stdlib.h> int main ()  {   int no;   double *data_n;   printf...

1 minute read.

Simple hash() function in C

Introduction In this context, we briefly discuss HASH FUNCTION, HASHING or HASH TABLE in C. It is a function used to map data and mapped arbitrary sizes to the fixed-size values. The...

7 minutes read.

#include in C

In the C programming language, #include is another way of inferring a standard, or a user defines file into the application or program. The preprocessor of the programming standard generally...

4 minutes read.

Builtin functions of GCC compiler

Certain built-in functions are present in the GCC compiler. These features are as follows: Function_ built in_popcount (x) The number of 1s in data of the integer type can be counted using...

3 minutes read.

Derived Data Types in C

A variable in a program occupies some space in the computer's memory where some value is stored. Each variable in C has an associated data type. A value to be...

4 minutes read.

Else If Ladder in C

What is Else If Ladder: When there are multiple options and a user has to decide from the options, we use Else If Ladder. Basically, it is an extension of if...

3 minutes read.

Variable in C

Variable is an identifier that holds data in memory. It is used to identify input data in a program. The value of the variable can change at the time of...

2 minutes read.

User Defined Functions in C

C Functions: A function is defined as a block of code that is used to perform some specific task. In functions, we use flower brackets ({}) which contain the set of programming...

5 minutes read.

Types of Array in C

What is an Array? One value can be stored in a variable at once. How many variables will you need if you have 100 values? Well, the answer isn't 100. It...

14 minutes read.

For Loop in C

The Syntax of For Loop for (initialization statement; test expression; update statement) {     /* main body of the FOR loop */ } How does For loop work? In for loop, the initialization command is...

3 minutes read.

strtok() and strtok_r() functions in C with examples

strtok() and strtok_r() functions in C with examples C offer various strtok() and strtok r() methods for a string to be separated by a certain delimiter. Dividing a string is a...

2 minutes read.

Heap Sort in C

In this tutorial, we will learn about heap sorting in C language, but before going to Heap sort, we have to know the concept of Complete Binary Tree. What is Complete...

8 minutes read.

While Loop in C programming examples

Introduction There is always a header file containing all the essential data regarding inputs and outputs of various functions in the C programs. The following statement describes how to use/add header file...

22 minutes read.

GCD program in C

C language : Dennis Ritchie developed the general-purpose computer language C at Bell Laboratories in 1972. Despite being an ancient language, it is extremely popular. It is among the most widely used...

4 minutes read.

Branching Statements in C

What is branching in c? Branching gets its name from the fact that the computer can select which branch to follow.Programs written in the C language execute statements one after the...

6 minutes read.

Decimal to Binary in C

What is a decimal number? A decimal number is a number represented in the decimal number system. This system of binary conversion uses base 10 to represent numbers, i.e. the digits...

3 minutes read.

Find a subarray with a given sum.

Find a subarray with a given sum. The simple solution is to recognize all subarrays one by one and to check each subarray's sum. The quick solution follows the following program. Algorithm: From...

4 minutes read.

How to Avoid Structure Padding in C

Generally, when an object of some structure type is declared, some contiguous memory will be allocated in block form, which will be allocated to structure members. To understand structure padding...

3 minutes read.

Types of Pointers in C

Pointers in C A pointer is a variable and this variable contains the address of another variable, i.e it’s a variable which has the address of another variable as its value....

4 minutes read.

Types Of Structures In C

We can normally store elements of the same datatype with the help of an array in C programming. We can store multiple numbers of elements of a character data type...

3 minutes read.