w3resource

C Programming Exercises, Practice, Solution : Date Time

C Date Time [10 exercises with solution]

[An editor is available at the bottom of the page to write and execute the scripts. Go to the editor]

1. Write a program in C to print the current date and time.

Expected Output :

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

Click me to see the solution

2. Write a program in C to compute the number of seconds passed since the beginning of the month.

Expected Output :

 222084 seconds passed since the beginning of the month.

Click me to see the solution

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

Expected Output :

Thu Aug 03 13:44:49 2017

Click me to see the solution

4. Write a program in C to convert a tm object to a custom textual representation.

Expected Output :

 The textual representation of specified date and time :                                                                      
September Sun Sep  2 16:30:32 2016 pm                                                                                         
September Sun Sep  2 16:30:32 2016 pm 

Click me to see the solution

5. Write a program in C to convert a tm object to a custom wide string textual representation.

Expected Output :

 The textual representation of specified date and time :

Sunday 09/02/16 17:51:10
Sunday 09/02/16 17:51:10

Click me to see the solution

6. Write a program in C to convert a time_t object to calendar time expressed as Coordinated Universal Time.

Expected Output :

The calendar time expressed as Coordinated Universal Time is :
UTC:   Thu Aug 03 10:53:03 2017
local: Thu Aug 03 16:23:03 2017

Click me to see the solution

7. Write a program in C to convert a time_t object to calendar time expressed as local time.

Expected Output :

The calendar time expressed as a local Time is :
UTC:   Thu Aug 03 11:15:59 2017
local: Thu Aug 03 16:45:59 2017

Click me to see the solution

8. Write a program in C to print the date and time before 24 months.

Expected Output :

Today is :          Thu Aug  3 17:27:16 2017                                                                                  
(DST is not in effect)                                                                                                        
                                                                                                                              
24 months ago the date was : Mon Aug  3 17:27:16 2015                                                                         
(DST was not in effect)

Click me to see the solution

9. Write a program in C to show the first of calendar time.

Expected Output :

Sun Jan 01 00:00:00 1900

Click me to see the solution

10. Write a program in C to show the start of the epoch.
Note : epoch means the beginning of a period in the history of someone.

Expected Output :

0 seconds since the epoch began
Thu Jan 01 00:00:00 1970

Click me to see the solution

C Programming Code Editor:

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.



Follow us on Facebook and Twitter for latest update.

C Programming: Tips of the Day

Returning an array using C

You can't return arrays from functions in C. You also can't (shouldn't) do this:

char *returnArray(char array []){
 char returned [10];
 //methods to pull values from array, interpret them, and then create new array
 return &(returned[0]); //is this correct?
} 

returned is created with automatic storage duration and references to it will become invalid once it leaves its declaring scope, i.e., when the function returns.

You will need to dynamically allocate the memory inside of the function or fill a preallocated buffer provided by the caller.

Dynamically allocate the memory inside of the function (caller responsible for deallocating ret)

char *foo(int count) {
    char *ret = malloc(count);
    if(!ret)
        return NULL;

    for(int i = 0; i < count; ++i) 
        ret[i] = i;

    return ret;
}

Call it like so:

int main() {
    char *p = foo(10);
    if(p) {
        // do stuff with p
        free(p);
    }

    return 0;
}

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





We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook