Compiling C codes in C++, using C++ wrappers for C time and date manipulation program example


Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TC)

Other info: none

To do: Compiling C codes in C++, using C++ wrappers for C time and date manipulation program example

To show: How to use C headers/functions in C++ codes demonstrating the C time and date manipulations




// C run-time Microsoft C example, compiled as C++ codes

// using the C++ wrappers i.e. using C header files in C++ code

#include <cstdio>

#include <ctime>


int main(void)


struct tm *time_now;

time_t secs_now;

time_t t;

char str[80];



// display current time and date, using ctime() though deprecated

printf("Today's date and time: %s\n", ctime(&t));


// sets time environment variables used by the _ftime() and localtime() functions to make corrections from

// coordinated universal time (UTC) to local time, and by the time function to compute UTC from system time.

// This POSIX function is deprecated. Use the ISO C++ conformant _tzset() instead.



time_now = localtime(&secs_now);


// formatting the time for output, using strftime()

strftime(str, 80,"It is %M minutes after %I o'clock, %A, %B %d 20%y\n", time_now);

printf("%s\n", str);


// computes the difference between two times, using difftime()

time_t first, second;


// gets system time

first = time(NULL);


// Waits for secs



// gets system time again

second = time(NULL);


printf("The difference is: %f seconds\n", difftime(second, first));


return 0;



Output example:


Today's date and time: Sun Nov 12 12:19:39 2006

It is 19 minutes after 12 o'clock, Sunday, November 12 2006

The difference is: 4.000000 seconds

Press any key to continue . . .



C and C++ Programming Resources | C & C++ Code Example Index