Use of Gets Function in C Programming

CCBeginner
Practice Now

Introduction

In C Programming language, gets() function is used to take input from the user. Unlike scanf(), gets() reads a whole line of text, stops reading when Enter key is pressed, and does not discard the newline character.

In this lab, we will learn how to use the gets() function in C programming through step-by-step instructions.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/data_types("`Data Types`") c/BasicsGroup -.-> c/operators("`Operators`") c/FunctionsGroup -.-> c/function_declaration("`Function Declaration`") subgraph Lab Skills c/output -.-> lab-123354{{"`Use of Gets Function in C Programming`"}} c/data_types -.-> lab-123354{{"`Use of Gets Function in C Programming`"}} c/operators -.-> lab-123354{{"`Use of Gets Function in C Programming`"}} c/function_declaration -.-> lab-123354{{"`Use of Gets Function in C Programming`"}} end

Setting up the environment

The first step is to open your text editor and create a new file. Save the empty file as main.c in the ~/project/ directory.

Writing the initial code

In this step, we need to write the initial code that includes headers, main function, and printf statement.

Copy the following code snippet and paste it into the main.c file.

#include <stdio.h>

int main()
{
    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
    return 0;
}

This code initiates a program and displays "Studytonight - Best place to learn" on the console.

Defining variable and taking input using gets() function

In this step, we will define a character array name and take input from the user using the gets() function.

Add the following code snippet in the main() function.

char name[50];

printf("Please enter your name: ");
gets(name);
printf("\nWelcome %s to the Studytonight Lab.", name);

This code snippet creates a char array name and uses gets() function to take input from the user and stores input in name array. The printf() function displays welcome message.

Printing the output

Add the code snippet below to print output on the console.

printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;

Final code

#include <stdio.h>

int main()
{
    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

    char name[50];
    printf("Please enter your name: ");
    gets(name);
    printf("\nWelcome %s to the Studytonight Lab.", name);

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");
    return 0;
}

Summary

In this lab, we learned how to use the gets() function in C programming to take input from the user. Here are some key points that we learned:

  • gets() function in C is used to take input from the user.
  • gets() function takes input until the Enter key is pressed.
  • gets() function stores the input along with the newline character in an array.

It is important to remember that gets() function is a security threat since it does not check the size of the array, which can lead to buffer overflow.

Other C Tutorials you may like