Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do stdin, stdout, stderr get closed?

Tags:

c

When you exit the program, how do these FILE* objects get closed and released?

like image 916
Paul Manta Avatar asked Jun 21 '26 10:06

Paul Manta


2 Answers

They are closed by the C runtime code which is automatically linked to your program - the code that calls your main() function also calls exit() after main() returns.

like image 75
Max Avatar answered Jun 23 '26 13:06

Max


From C99 §7.20.4.3/3:

Next, all open streams with unwritten buffered data are flushed, all open streams are closed, and all files created by the tmpfile function are removed.

POSIX (aligned with C99) spells it out better:

The exit() function shall then flush all open streams with unwritten buffered data and close all open streams.

like image 45
cnicutar Avatar answered Jun 23 '26 13:06

cnicutar