banner



How To Use Gets In C

Gets() And Fgets() In C

Introduction

We all are familiar with the scanf() part. It is the chief function applicable to take basic user inputs. Even though scanf() works not bad while taking inputs such as integer, character, bladder etc. Information technology certainly falls behind while taking cord inputs containing whitespaces. Let'south have a await at an instance,

#include<stdio.h> int master() {     char string[10];     printf("Enter the string: ");     scanf("%s", string);     printf("\due north %southward",string);     render 0; }                

Output:

Problem With Scanf
Problem With scanf()

Every bit we can observe from the above example. scanf() stops scanning as soon as it encounters whitespace or newline. This, in fact, makes taking cord inputs using scanf() a bit troublesome. This tin be hands avoided past using another input functions like gets() and fgets().

In this article, we are going to learn how to apply both the functions and compare them side by side.

gets() function in C

gets() is a pre-divers part in C which is used to read a cord or a text line. And store the input in a well-defined string variable. The function terminates its reading session equally soon equally information technology encounters a newline character.

Syntax:

gets( variable proper name );

The given code below illustrates the use of the gets() part,

#include<stdio.h> int primary() {     char string[10];     printf("Enter the String: ");     gets(cord);     printf("\n%due south",string);     return 0; }                

Output:

Use Of Gets()
Employ Of Gets

Compare the output with the one while using scanf(). 'Hello World' is now treated as a single cord.

fgets() function in C

The standard C library also provides u.s. with however another function, the fgets() function. The function reads a text line or a string from the specified file or console. And then stores information technology to the respective string variable.

Similar to the gets() part, fgets also terminates reading whenever it encounters a newline character. Just furthermore, unlike gets(), the function too stops when EOF is reached or fifty-fifty if the string length exceeds the specified limit, due north-1.

Syntax,

fgets(char *str, int northward, FILE *stream)

  • str – It is the variable in which the string is going to be stored
  • n – It is the maximum length of the string that should exist read
  • stream – It is the filehandle, from where the string is to exist read.

Fortunately, we tin can both read text lines from a file or the standard input stream by using the fgets() function. Allow us see how

1. Read from a given file using fgets()

For instance,

#include<stdio.h> int main() {     char string[20];     FILE *fp;     fp=fopen("file.txt","r");     fgets(string,xx,fp);     printf("The string is: %southward",string);     fclose(fp);     return 0; }                

Consider file.txt to comprise the line 'JournalDev fgets() instance!'. In that case, the output of the above code would be,

Fgets Output
fgets() file input

2. Read from stdin using fgets()

#include<stdio.h> int primary() {     char cord[20];     printf("Enter the cord: ");     fgets(string,20,stdin);         #input from stdin stream     printf("\nThe string is: %s",string);     return 0; }                

Output:

Fgets() Stdin Input
fgets() Stdin Input

Conclusion

Even though both the functions, gets() and fgets() tin can be used for reading string inputs. The biggest difference between the two is the fact that the latter allows the user to specify the buffer size. Hence it is highly recommended over the gets() role.

The gets() function doesn't have the provision for the instance if the input is larger than the buffer. As a effect, memory clogging may occur. This is the part where the fgets() office shines and provides an ultimate solution.

References

  • https://www.journaldev.com/35071/strings-in-c-programming
  • https://stackoverflow.com/questions/19748307/employ-of-fgets-and-gets

Source: https://www.journaldev.com/35456/fgets-and-gets-in-c-programming

Posted by: mattisonsaystim.blogspot.com

0 Response to "How To Use Gets In C"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel