Paul Manta
Paul Manta

Reputation: 31597

How do stdin, stdout, stderr get closed?

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

Upvotes: 2

Views: 449

Answers (2)

Max
Max

Reputation: 2131

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.

Upvotes: 6

cnicutar
cnicutar

Reputation: 182664

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.

Upvotes: 4

Related Questions