Scroll Down to See All
abortabsacosasctimeasctime_rasinassertatanatan2atexitatofatoiatolbsearchbtowccalloccatclosecatgetscatopenceilclearerrclockcoscoshctimectime_rdifftimediverferfcexitexpfabsfdopenfeofferrorfflushfgetcfgetposfgetsfgetwcfgetwsfopenfprintffputcfputwsfreadfreefreopenfrexpfscanffseekfsetposftellfwidefwprintffwritefwscanfgetcgetchargetenvgetwcgetwchargmtimegmtime_rhypotisalnumisalphaisasciiisblankiscntrlisdigitisgraphislowerisprintispunctisspaceisupperiswalnumiswalphaiswblankiswcntrliswctypeiswdigitiswgraphiswloweriswprintiswpunctiswspaceiswupperiswxdigitisxdigitj0j1jnlabsldexpldivlocaleconvlocaltimelocaltime_rloglog10longjmpmallocmblenmbrlenmbrtowcmbsinitmbsrtowcsmbstowcsmbtowcmemchrmemcmpmemcpymemmovememsetmktimemodfnextafternextafterlnexttowardnexttowardlnl_langinfoperrorpowprintfputcputcharputenvputsputwcputwcharqsortquantexpd32quantexpd64quantexpd128quantized32quantized64quantized128samequantumd32raiserandrand_rreallocregcompregerrorregexecregfreeremoverenamerewindscanfsetbufsetjmpsetlocalesetvbufsignalsinsinhsnprintfsprintfsqrtsrandsscanfstrcasecmpstrcatstrchrstrcmpstrcollstrcpystrcspnstrerrorstrfmonstrftimestrlenstrncasecmpstrncatstrncmpstrncpystrpbrkstrptimestrrchrstrspnstrstrstrtodstrtod32strtod64strtod128strtofstrtokstrtok_rstrtolstrtoldstrtoulstrxfrmswprintfswscanfsystemtantanhtimetime64tmpfiletmpnamtoasciitolowertouppertowctranstowlowertowupperungetcungetwcva_argva_copyva_endva_startvfprintfvfscanfvfwprintfvfwscanfvprintfvscanfvsprintfvsnprintfvsscanfvswprintfvswscanfvwprintfvwscanfwcrtombwcscatwcschrwcscmpwcscollwcscpywcscpywcsftimewcslenwcsncatwcsncmpwcsncpywcspbrkwcsptimewcsrchrwcsrtombswcsspnwcsstrwcstodwcstod32wcstod64wcstod128wcstofwcstokwcstolwcstoldwcstombswcstoulwcsxfrmwctobwctombwctranswctypewcwidthwmemchrwmemcmpwmemcpywmemmovewmemsetwprintfwscanfy0y1yn



Function Details : fclose

intfclose(FILE * stream) ;

Return Type : int

Integer type (typically 4 bytes, -2,147,483,648 to 2,147,483,647)
Read about return values of fclose function .

1st Parameter Type : FILE *

Pointer to FILE structure for file operations

1st Parameter

Pointer to the file stream to be closed
The FILE pointer parameter in fclose must:
● Be a valid pointer from a previous successful fopen call;
● Not be NULL (causes undefined behavior);
● Not be already closed (undefined behavior);
● Not be modified/corrupted since opening;
Key points:
● Only one parameter needed, no additional flags/options;
● Must match exactly with fopen's returned pointer;
● Cannot be reused after fclose call;
● No validation provided - programmer must ensure validity;
The parameter's state after fclose:
● Pointer becomes invalid;
● Any further use is undefined behavior;
● Original pointer value remains but shouldn't be used;
● Should typically be set to NULL after closing;


Read more about parameters of fclose in parameters section
The fclosefunction in C language terminates a file stream by flushing any buffered data and releasing system resources.
fclose is an essential stream management function in C language that properly terminates file operations. It works in tandem with fopen to form the basic file handling pair - any file opened with fopen must be closed with fclose. It's particularly important for its buffer flushing behavior, ensuring all pending write operations complete before closing. The function interacts with the whole stdio.h family (fopen, fwrite, fread, fseek, etc.) and is crucial for system resource management since it releases file descriptors and memory used for stream buffers.
What makes fclose special is that it's not just a system close - it first ensures all buffered data is written (which flushing can fail), and only then closes the underlying file descriptor. This two-step process is why error checking its return value is important, unlike many other closing operations in C programming language.

To Summarize the Workflow:
  1. 1.
    Takes one parameter: pointer to FILE structure
  2. 2.
    Flushes unwritten buffered data before closing
  3. 3.
    Closes the stream and disassociates it from the file
  4. 4.
    Returns 0 on success, EOF on error
  5. 5.
    Note: Files are automatically closed on program termination if not explicitly closed


Yes

No

fclose(FILE *stream)

Is stream valid?

Flush buffers

Close stream

Return 0 Success

Return EOF Error




Read more about return type and value offclose function in return section.
The fclosefunction takes 1 parameter:
  • FILE * `stream`: Pointer to the file stream to be closed
    The FILE pointer parameter in fclose must:
    ● Be a valid pointer from a previous successful fopen call;
    ● Not be NULL (causes undefined behavior);
    ● Not be already closed (undefined behavior);
    ● Not be modified/corrupted since opening;
    Key points:
    ● Only one parameter needed, no additional flags/options;
    ● Must match exactly with fopen's returned pointer;
    ● Cannot be reused after fclose call;
    ● No validation provided - programmer must ensure validity;
    The parameter's state after fclose:
    ● Pointer becomes invalid;
    ● Any further use is undefined behavior;
    ● Original pointer value remains but shouldn't be used;
    ● Should typically be set to NULL after closing;

The fclose function return value :
  • Returns 0 if the stream is successfully closed
    On failure, it returns EOF and sets errno to indicate the error


Yes

No

Yes

No

fclose(FILE *stream)

Buffer flush successful?

Close stream successful?

Return 0

Return EOF


Output

This example demonstrates the basic usage of `fclose`. It opens a file, writes to it, and then closes it using fclose. It also checks for errors during closing.