[Courses] Reminder, and questions (and semi spoiler on ex 6-1)

Akkana akkana at shallowsky.com
Mon Feb 25 13:28:22 EST 2002


Michelle Murrain writes:
> 1) Why is forgetting the "\n" at the end of a printf statement suppress the 
> line altogether? ( in perl, it will simply print the line w/o a newline).

In addition to the answers that have already been posted, there's
one more subtlety: on most systems, output to the terminal from stdio
functions like printf is "line buffered", which means that it doesn't
actually print anything until it has a whole line to print.  (Sometimes
the buffer has a maximum size, so it will show the output when it gets
to 512 characters or something even if it hasn't seen a newline, but
you can't count on it.)

So if you're not seeing the output line when it doesn't end with a \n,
that might be why.  It should flush its buffers when the program exits,
but if you kill the program with ^C you may never see the output.

If you ever need to printf a line that doesn't end with a newline
and see the output immediately, you can add this after the printf:

	fflush(stdout);

That tells the stdio library to flush its buffers immediately.

	...Akkana



More information about the Courses mailing list