fgets() and gets() In C language https://www.skillvertex.com/blog Wed, 06 Mar 2024 11:35:32 +0000 en-US hourly 1 https://wordpress.org/?v=6.6.1 https://www.skillvertex.com/blog/wp-content/uploads/2024/01/favicon.png fgets() and gets() In C language https://www.skillvertex.com/blog 32 32 fgets() and gets() In C language https://www.skillvertex.com/blog/fgets-and-gets-in-c-language/ https://www.skillvertex.com/blog/fgets-and-gets-in-c-language/#respond Wed, 06 Mar 2024 11:35:32 +0000 https://www.skillvertex.com/blog/?p=3577 Read more]]>

Table of Contents

fgets() and gets() In C language

Both gets() and fgets() are functions in C used for reading strings with spaces, but they have some key differences:

fgets()

the fgets() function reads a line from the specified stream and stores it into the string pointed to by the provided buffer (str). It stops reading under the following conditions:

  1. It reads (n-1) characters, where n is the size of the buffer. This ensures that the last character in the buffer is reserved for the null terminator ('\0'), making the string properly terminated.
  2. It encounters a newline character ('\n') in the input, indicating the end of the line.
  3. It reaches the end-of-file (EOF).

This behavior of fgets() is valuable for reading lines of text while providing control over the number of characters read and preventing buffer overflow. Developers commonly use fgets() for safe and controlled string input in C programming.

Syntax

char *fgets (char *str, int n, FILE *stream);

Parameter

  • str: It’s where the read string is stored. This is a pointer to an array of characters.
  • n: This is the maximum number of characters that can be copied into str, including the final null character.
  • stream: It’s a pointer to a FILE object, which points to the source of input, like a file or standard input.

Return Value

The fgets() function will returns a pointer to the string where the input is stored.

Features of fgets()

  • Parameters: fgets() uses parameters like the maximum length (how many characters to read), a buffer (where the string is kept), and a reference to where the input comes from.
  • Safety: It’s safe to use because it checks how much space is available in the buffer, preventing it from grabbing too many characters and causing problems.
  • Reading Behavior: fgets() keeps reading characters until it finds a new line or reaches the maximum limit set by the buffer size. This helps control and manage string input in a safe way.

Example of fgets()

 Consider the maximum number of characters is 15 and the input length is greater than 15 but still fgets() will read only 15 characters and print it. 

// C program to illustrate fgets()
#include <stdio.h>
#define MAX 15
 
int main()
{
    // defining buffer
    char buf[MAX];
 
    // using fgets to take input from stdin
    fgets(buf, MAX, stdin);
    printf("string is: %s\n", buf);
 
    return 0;
}
Input:
Hello and welcome to Skillvertex

Output:
string is: Hello and welc

gets()

  • Functionality: It reads characters from the keyboard and puts them into a C string until it sees a new line or reaches the end of the input.
  • Safety Concerns: Be cautious using it because it doesn’t check how much space is available, which can cause problems by using too much space.
  • Usage: It’s used to get strings from the user until they press “Enter.”

Syntax

char *gets( char *str );

Parameters

str is a pointer to a block of memory, typically an array of characters, where the read string is stored as a C string.

Return Value

The function will return a pointer to the string where input is being stored.

Example of gets()

Consider a character array of 15 characters and the input will be greater than 15 characters, gets() reads all these characters and store them into a variable. Since, gets() does not check the maximum limit of input characters, at any time compiler may return buffer overflow error. 

// C program to illustrate
// gets()
#include <stdio.h>
#define MAX 15
 
int main()
{
    // defining buffer
    char buf[MAX];
 
    printf("Enter a string: ");
 
    // using gets to take string from stdin
    gets(buf);
    printf("string is: %s\n", buf);
 
    return 0;
}
Input:
Hello and welcome to Skillvertex 

Output:
Hello and welcome to Skillvertex

FAQ -fgets() and gets() In C language

Q1. What is the use of gets () function example?

Ans. gets() reads a line from the standard input, storing it in a buffer. The line consists of characters until the first newline or EOF. If a newline is encountered, gets() replaces it with a null character before returning the line. Note that gets() lacks input boundary checking, making it unsafe; it is deprecated in modern C, and safer alternatives like fgets() are recommended.

Q2.What are the 4 functions in C?

Ans.Printf(), scanf(), ceil(), and floor() are examples of library functions.

Q3.What is the syntax of gets in C?

Ans.char *gets(char *str) 

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

]]>
https://www.skillvertex.com/blog/fgets-and-gets-in-c-language/feed/ 0