fclose() Function in C Last Updated : 23 Jul, 2025 Comments Improve Suggest changes 1 Likes Like Report In C language, fclose() is a standard library function used to close a file that was previously opened using fopen(). This function is the itegral part of the file handling in C which allows the users to free the memory occupied by the file once all the operations are done on it.In this article, we will learn about the fclose() function, its syntax and how it behaves in different scenarios.Syntax of fclose()The syntax of fclose is straigforward:fclose(file_pointer);Parametersfile_pointer: A pointer to a FILE object that identifies the stream to be closed.Return ValueThis function only returns two values:0: When the file is successfully closed.EOF: When an error occurred while closing the file.Example of fclose() in C C // C program to demonstrate the use of fclose() #include <stdio.h> int main() { // Open the file for writing FILE *file_ptr = fopen("example.txt", "w"); if (file_ptr == NULL) { perror("Error opening file"); return 1; } // Write some data to the file fprintf(file_ptr, "Hello, World!\n"); // Close the file and check for errors if (fclose(file_ptr) != 0) { perror("Error closing file"); return 1; } printf("File operations completed successfully.\n"); return 0; } OutputFile operations completed successfully. Note: Using the file stream after it has been closed with fclose() results in undefined behavior. This means that any operations on the closed file stream can lead to unpredictable results, including crashes or data corruption.Related Articles:Basics of File Handling in CC fopen() Function in C Create Quiz Comment A abhishekcpp Follow 1 Improve A abhishekcpp Follow 1 Improve Article Tags : C Language C-Functions C-File Handling Explore C BasicsC Language Introduction6 min readIdentifiers in C3 min readKeywords in C2 min readVariables in C4 min readData Types in C3 min readOperators in C8 min readDecision Making in C (if , if..else, Nested if, if-else-if )7 min readLoops in C6 min readFunctions in C5 min readArrays & StringsArrays in C4 min readStrings in C5 min readPointers and StructuresPointers in C7 min readFunction Pointer in C5 min readUnions in C3 min readEnumeration (or enum) in C5 min readStructure Member Alignment, Padding and Data Packing8 min readMemory ManagementMemory Layout of C Programs5 min readDynamic Memory Allocation in C7 min readWhat is Memory Leak? How can we avoid?2 min readFile & Error HandlingFile Handling in C11 min readRead/Write Structure From/to a File in C3 min readError Handling in C8 min readUsing goto for Exception Handling in C4 min readError Handling During File Operations in C5 min readAdvanced ConceptsVariadic Functions in C5 min readSignals in C language5 min readSocket Programming in C8 min read_Generics Keyword in C3 min readMultithreading in C9 min read Like