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 likeprintf
.#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 structuretimespec
variable namedc
. 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 variablec
. TheCLOCK_REALTIME
constant represents the system's real-time clock.printf("Seconds: %ld\n", c.tv_sec);
: Prints the seconds component of thec
variable using theprintf
function and the%ld
format specifier for along int
.printf("Nanoseconds: %ld\n", c.tv_nsec);
: Prints the nanoseconds component of thec
variable using theprintf
function and the%ld
format specifier for along int
.return 0;
: Indicates successful program execution by returning zero from themain()
function.