C Interview Questions and Answers


How can I implement a delay, or time a users response, with sub-second resolution?

Unfortunately, there is no portable way. Routines you might look for on your system
include clock, delay, ftime, gettimeofday, msleep, nap, napms, nanosleep, setitimer,
sleep, Sleep, times, and usleep. (A function called wait, however, is at least under
Unix not what you want.) The select and poll calls (if available) can be pressed
the system timer and timer interrupts.

Of these, only clock is part of the ANSI Standard. The difference between two calls
to clock gives elapsed execution time, and may even have subsecond resolution, if
CLOCKS_PER_SEC is greater than 1. However, clock gives elapsed processor time used
by the current program, which on a multitasking system (or in a non-CPU-intensive
program) may differ considerably from real time.

on a single-user, single-tasking machine, as it is terribly antisocial to any other
your process to sleep for the duration, such as sleep or select, or pause in conjunction
with alarm or setitimer.


long int i;



Posted by:Richards