I'm not sure (its been awhile since I've done this level of coding) maybe you need to flush the buffer. If you replace printf with fflush(); are the results the same?

Cheers,
-D