Category Archives: C

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

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

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 : 

The program returns 16 on my platform. 

Best way to detect integer overflow

On assignment C-bootcamp1, we were asked to write a factorial function.
The difficult part is how to detect integer overflow.

Defective idea
After some research with Google, I found a post on StackOverflow.com, which guides me to fault. I can’t find the original post though.

It says, when integer overflow occurs, the result(n * (n-1)) is smaller than n.
Because, int, on a 32 bit machine, is 32bits, the return value is truncated when it needs more bits, namely when it is Continue reading

Is malloc/calloc evaluated at runtime?

Problem:

When I compile it, it gives error: Continue reading