Scroll Down to See All
abortabsacosasctimeasctime_rasinassertatanatan2atexitatofatoiatolbsearchbtowccalloccatclosecatgetscatopenceilclearerrclockcoscoshctimectime_rdifftimediverferfcexitexpfabsfclosefdopenfeofferrorfflushfgetcfgetposfgetwcfgetwsfopenfprintffputcfputwsfreadfreefreopenfrexpfscanffseekfsetposftellfwidefwprintffwritefwscanfgetcgetchargetenvgetwcgetwchargmtimegmtime_rhypotisalnumisalphaisasciiisblankiscntrlisdigitisgraphislowerisprintispunctisspaceisupperiswalnumiswalphaiswblankiswcntrliswctypeiswdigitiswgraphiswloweriswprintiswpunctiswspaceiswupperiswxdigitisxdigitj0j1jnlabsldexpldivlocaleconvlocaltimelocaltime_rloglog10longjmpmallocmblenmbrlenmbrtowcmbsinitmbsrtowcsmbstowcsmbtowcmemchrmemcmpmemcpymemmovememsetmktimemodfnextafternextafterlnexttowardnexttowardlnl_langinfoperrorpowprintfputcputcharputenvputsputwcputwcharqsortquantexpd32quantexpd64quantexpd128quantized32quantized64quantized128samequantumd32raiserandrand_rreallocregcompregerrorregexecregfreeremoverenamerewindscanfsetbufsetjmpsetlocalesetvbufsignalsinsinhsnprintfsprintfsqrtsrandsscanfstrcasecmpstrcatstrchrstrcmpstrcollstrcpystrcspnstrerrorstrfmonstrftimestrlenstrncasecmpstrncatstrncmpstrncpystrpbrkstrptimestrrchrstrspnstrstrstrtodstrtod32strtod64strtod128strtofstrtokstrtok_rstrtolstrtoldstrtoulstrxfrmswprintfswscanfsystemtantanhtimetime64tmpfiletmpnamtoasciitolowertouppertowctranstowlowertowupperungetcungetwcva_argva_copyva_endva_startvfprintfvfscanfvfwprintfvfwscanfvprintfvscanfvsprintfvsnprintfvsscanfvswprintfvswscanfvwprintfvwscanfwcrtombwcscatwcschrwcscmpwcscollwcscpywcscpywcsftimewcslenwcsncatwcsncmpwcsncpywcspbrkwcsptimewcsrchrwcsrtombswcsspnwcsstrwcstodwcstod32wcstod64wcstod128wcstofwcstokwcstolwcstoldwcstombswcstoulwcsxfrmwctobwctombwctranswctypewcwidthwmemchrwmemcmpwmemcpywmemmovewmemsetwprintfwscanfy0y1yn



Function Details : fgets

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

Return Type : char *

String pointer (array of characters)
Read about return values of fgets function .

1st Parameter Type : char *

String pointer (array of characters)

1st Parameter

Pointer to the buffer where the input string will be stored

2nd Parameter Type : int

Integer type (typically 4 bytes, -2,147,483,648 to 2,147,483,647)

2nd Parameter

The maximum number of characters to read, including the null terminator

3rd Parameter Type : FILE *

Pointer to FILE structure for file operations

3rd Parameter

Pointer to the file stream from which the input is read

Read more about parameters of fgets in parameters section
The fgetsfunction in C language reads a line from the specified stream and stores it into the string pointed to by str, up to the user specified number of characters.
fgets is a secure line-reading function in C, designed specifically for safely reading strings with a buffer size limit to prevent overflow. Unlike gets (which is now deprecated due to security issues), fgets requires explicit buffer size specification and preserves newline characters. It is commonly used in conjunction with file I/O functions like fopen for reading files line by line, and error-checking functions like ferror and feof to handle read failures. The function is particularly useful in text processing applications where line-by-line reading is needed, and it's often preferred over character-by-character reading functions like getc when dealing with text files.
This function is special because:
• It handles buffer boundaries safely;
• It preserves newline characters (unlike some other string functions);
• It works with any stream (not just stdin);
• It's part of the standard C library's safer string handling functions;

To Summarize the Workflow:
  1. 1.
    Function reads a line from specified stream into given string buffer
  2. 2.
    Takes three parameters: destination buffer (str), max characters to read (n), and input stream
  3. 3.
    Reads until: newline character found, n-1 characters read, or EOF reached
  4. 4.
    Adds null terminator after last character read
  5. 5.
    Returns str pointer on success, NULL on EOF or error
  6. 6.
    Includes newline character in output string if found


Success

EOF/Error

Yes

No

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

Read attempted

Process characters

Found or n-1 chars?

Add null terminator

Return str pointer

Return NULL




Read more about return type and value offgets function in return section.
The fgetsfunction takes 3 parameters:
  • char * `str`: Pointer to the buffer where the input string will be stored
  • int `n`: The maximum number of characters to read, including the null terminator
  • FILE * `stream`: Pointer to the file stream from which the input is read
Reads at most `n-1` characters from the specified file stream and stores them in the buffer pointed to by `str`. The function stops reading on encountering a newline character or the end-of-file. The resulting string is null-terminated.
The fgets function return value :
  • On success, the function returns str;
  • If the end-of-file is encountered while attempting to read a character, the EOF indicator is set (feof);
  • If this happens before any characters could be read, the pointer returned is a null pointer (and the contents of str remain unchanged);
  • If a read error occurs, the error indicator (ferror) is set and a null pointer is also returned;


Characters read

Failed

EOF

Error

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

Read operation result

Success: Return str pointer

Failure type

EOF before any read: Set EOF indicator Return NULL str unchanged

Read error: Set error indicator Return NULL str unchanged


Output

This example demonstrates the basic usage of `fgets`. It reads a file line by line and prints each line to the console.