Click here to Skip to main content
15,877,754 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm reading this code on Ubuntu:

/**
  add time header
*/
static void _add_time_header( struct evbuffer *buf )
{
        char date[50];
#ifndef WIN32
        struct tm cur;
#endif
        struct tm *cur_p;
        time_t t = time( NULL );
#ifdef WIN32
        cur_p = gmtime( &t );
#else
        gmtime_r( &t, &cur );
        cur_p = &cur;
#endif
        strftime( date, sizeof( date ), "%a, %d %b %Y %H:%M:%S GMT", cur_p);
        evbuffer_add_printf( buf, "Date: %s\r\n", date );
}


I don't understand why the coder use #ifndef WIN32 ,
I have read the manual about gmtime and gmtime_r, but I couldn't find anything.
Please clarify me why?

Thank you
Posted
Updated 10-Feb-11 20:15pm
v2

1 solution

#ifndef is an instruction to part of the compiler (called the preprocessor) to only include the code if a symbol (in this case "WIN32") is not defined. Equally, #ifdef includes the code if the symbol is defined.

All it does is makes it possible to have one code file which can cope with different compilation circumstances.

The most common use is probably
#ifdef DEBUG
to only include tracing or logging statements if this is the test version rather than release. That way, when there is a problem, the same code gets tested with tracing code inserted, but without changing the source.

In the code fragment you show, #ifddef WIN32is there to let the code work the same with two different systems Time formats: Ubuntu and Windows (which handle it a little differently).
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 11-Feb-11 15:03pm    
Aha, a 5.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900