w3resource

C Exercises: Convert a time_t object to a textual representation

C Date Time: Exercise-3 with Solution

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

Sample Solution:

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
}

Sample Output:

Thu Aug 03 13:44:49 2017

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

Flowchart:

Flowchart: Convert a time_t object to a textual representation

C Programming Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C to compute the number of seconds passed since the beginning of the month.
Next: Write a program in C to convert a tm object to custom textual representation.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



C Programming: Tips of the Day

How I can print to stderr in C?

The syntax is almost the same as printf. With printf you give the string format and its contents i.e.:

printf("my %s has %d chars\n", "string format", 30);

With fprintf it is the same, except now you are also specifying the place to print to:

File *myFile;
...
fprintf( myFile, "my %s has %d chars\n", "string format", 30);

Or in your case:

fprintf( stderr, "my %s has %d chars\n", "string format", 30);

Ref : https://bit.ly/3v5U8Zq