w3resource logo


C Programming Exercises

C Exercises: Print the current time

C Date Time: Exercise-1 with Solution

Write a program in C to print the current time.

Sample Solution:

C Code:

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

int main(void)
    time_t cur_time;
    char* cur_t_string;
    cur_time = time(NULL);
    if (cur_time == ((time_t)-1))
        (void) fprintf(stderr, "Failure to get the current time.\n");
    cur_t_string = ctime(&cur_time); //convert to local time format
    if (cur_t_string == NULL)
        (void) fprintf(stderr, "Failure to convert the current time.\n");
    (void) printf("\n The Current time is : %s \n", cur_t_string);

Sample Output:

 The Current time is : Thu Aug 03 13:38:58 2017

N.B.: The result may varry for your current system date and time.


Flowchart: Print the current time

C Programming Code Editor:

Improve this sample solution and post your code through Disqus