w3resource logo


C Programming Exercises

C Exercises: Convert a time_t object to a textual representation

Secondary Nav

C Date Time: Exercise-3 with Solution

Write a program in C to convert a time_t object to a textual representation.

Expected Output :

Tue Sep  6 12:20:53 2016

C Code:

#define __STDC_WANT_LIB_EXT1__ 1
#include <time.h>
#include <stdio.h> 

int main(void)
{
    time_t result = time(NULL);
    printf("\n%s\n", ctime(&result));
#ifdef __STDC_LIB_EXT1__
    char time_str[26];
    ctime_s(time_str,sizeof time_str,&result);
    printf("\n%s\n", time_str);
#endif
}

Flowchart :

Flowchart: Convert a time_t object to a textual representation

C Practice online:

#define __STDC_WANT_LIB_EXT1__ 1
#include <time.h>
#include <stdio.h> 

int main(void)
{
    time_t result = time(NULL);
    printf("\n%s\n", ctime(&result));
#ifdef __STDC_LIB_EXT1__
    char time_str[26];
    ctime_s(time_str,sizeof time_str,&result);
    printf("\n%s\n", time_str);
#endif
}

Improve this sample solution and post your code through Disqus



Join our Question Answer community to learn and share your programming knowledge.

Help the community:

PHP: Loop

JavaScript: Delete duplicates in an array

Oracle: Round the number