timespec c

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

int main() {
    struct timespec c;

    clock_gettime(CLOCK_REALTIME, &c);

    printf("Seconds: %ld\n", c.tv_sec);
    printf("Nanoseconds: %ld\n", c.tv_nsec);

    return 0;
}

Explanation:

  • #include <stdio.h>: Includes the standard input/output header file, providing functions like printf.
  • #include <time.h>: Includes the time header file, which contains functions and data types related to time handling.
  • int main() { ... }: Defines the main function where the program's execution starts.
  • struct timespec c;: Declares a structure timespec variable named c. This structure holds time information with seconds and nanoseconds.
  • clock_gettime(CLOCK_REALTIME, &c);: Retrieves the current time from the system clock in real-time and stores it in the variable c. The CLOCK_REALTIME constant represents the system's real-time clock.
  • printf("Seconds: %ld\n", c.tv_sec);: Prints the seconds component of the c variable using the printf function and the %ld format specifier for a long int.
  • printf("Nanoseconds: %ld\n", c.tv_nsec);: Prints the nanoseconds component of the c variable using the printf function and the %ld format specifier for a long int.
  • return 0;: Indicates successful program execution by returning zero from the main() function.