A discussion about fclose(); Max number of open files allowed in a program

To close a file, use the fclose() function.

int fclose( FILE *fp );

The function flushes any data still pending in the buffer to the file, closes the file, and releases any memory used for the stream’s input and output buffers. The fclose() function returns zero on success, or EOF if an error occurs. 

When the program exits, all open field are closed automatically. Nonetheless, you should always close any file that you have finished processing. Otherwise, you risk losing data in the case of an abnormal program termination. Furthermore, there is a limit to the number of files that a program may have open at one time; the number of allowed open files is the value of the constant FOPEN_MAX.

To check out this value. we can do :

printf("Max # of open files = %d\n", FOPEN_MAX);

The program returns 16 on my platform.