C Tutorial

C Time

time.h measures clock time. time, difftime, and clock live in the standard library.

Calendar time and CPU time

time.h gives two different clocks. time reports calendar time: seconds since the Unix epoch, as a time_t. clock reports processor time used by this program, as aclock_t. difftime subtracts two time_t values and returns adouble number of seconds.

Include time.h. Keep the examples short. On a fast machine a tiny loop may show0.000000 CPU seconds. That still means the call worked.

time(NULL)

time(NULL) stores the current calendar time and returns it. Passing NULL means you do not also write it through a pointer. Cast to long to print: time_t is an integer type, but its width varies.

Example

#include <stdio.h>
#include <time.h>

int main(void) {
  time_t now = time(NULL);
  printf("time: %ld\n", (long)now);
  return 0;
}

Run this at /c/try. gcc compiles it. The printed number should grow if you wait a second and run again.

difftime

difftime(end, start) is end - start in seconds, as a double. Two calls to time back to back often differ by 0. That is expected: calendar time has one-second resolution on many systems.

Example

#include <stdio.h>
#include <time.h>

int main(void) {
  time_t start = time(NULL);
  time_t end = time(NULL);
  printf("difftime: %.0f\n", difftime(end, start));
  return 0;
}

Use difftime when you have two real timestamps — start of a job and end of a job — not when you need sub-second precision. For that, look at clock.

clock

clock() returns processor time since the program started, in ticks. Divide byCLOCKS_PER_SEC to get seconds. This loop does a little work so the interval is not always zero.

Example

#include <stdio.h>
#include <time.h>

int main(void) {
  clock_t t0;
  clock_t t1;
  long sum;
  int i;

  t0 = clock();
  sum = 0;
  for (i = 0; i < 1000000; i++) {
    sum += i;
  }
  t1 = clock();

  printf("sum: %ld\n", sum);
  printf("cpu seconds: %f\n", (double)(t1 - t0) / CLOCKS_PER_SEC);
  return 0;
}

Which call to use

CallMeasures
time(NULL)Calendar time as time_t
difftime(end, start)Seconds between two time_t values
clock()Processor ticks; divide by CLOCKS_PER_SEC

Do not treat clock as wall-clock time. It ignores time spent waiting. Next: string functions instring.h, which copy and compare null-terminated arrays.

Worked examples

The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.

Open them in the C editor at /c/try. Change one measurement and check whether the result still has the right unit.

Physics

CPU time for a running sum

Numerical methods get compared by error and by time. clock measures processor time for a tight loop, not wall time if the machine is busy with something else. The sum itself is 20.0 if you add 0.0001 two hundred thousand times — in exact decimal. Binary floating point will be close.

On a fast machine the reported cpu time may print as 0.000000. That is a coarse clock, not a proof the loop was skipped.

Example

#include <stdio.h>
#include <time.h>

int main(void) {
  clock_t start = clock();
  double sum = 0.0;
  int i;
  for (i = 0; i < 200000; i++) {
    sum += 0.0001;
  }
  clock_t end = clock();
  printf("sum = %.1f\n", sum);
  printf("cpu = %.6f s\n", (double)(end - start) / CLOCKS_PER_SEC);
  return 0;
}

Astronomy

Seconds in a day

A mean solar day is 86400 s by definition of how we count civil time. A sidereal day is about 86164 s. CLOCKS_PER_SEC tells you how clock() is scaled, not how long a day is. Both appear here because time.h is where clocks live, and days are what astronomers convert constantly.

1 d = 86400 s (civil)

Example

#include <stdio.h>
#include <time.h>

int main(void) {
  const double day_s = 86400.0;
  printf("1 day = %.0f s\n", day_s);
  printf("clock ticks per sec = %ld\n", (long)CLOCKS_PER_SEC);
  return 0;
}

FAQ: C Time

Common questions about this page.

What is the StudyGrid C tutorial?

The StudyGrid C tutorial is a full beginner-to-advanced track: syntax, types, input, loops, functions, pointers, structs, files, and the standard library. Each chapter has copy-and-run examples.

Should I run c time.h examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn c time.h in this C C lesson (C Time).

Is the C editor the same as Try Python, Try HTML, or Try C++?

No. Try C compiles with gcc at /c/try and shows stdout plus compiler messages. Try Python stays at /try. Try HTML stays at /html/try. Try C++ stays at /cpp/try. C lessons never open those editors.

Do I need to install a compiler to learn C?

No. Open a chapter, click Try it in C, and compile in the browser. You can also download a .c file and compile locally with gcc.

Where should I start the C tutorial?

Start at C Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After pointers, open C Examples, then files and bitwise. Use Next at the bottom of each chapter.

Is the C tutorial free?

Yes. The C workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.