Sign in to follow this  

can someone explain this

This topic is 3491 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

The # commands (#include, #if, #define) are all preprocessor directive. This means that, before your code gets actually compiled into machine code, all the # commands will be interpreted and affect your physical code. A simple "#define my_define 12" for example, will replace every instance of "my_define" in your code with a 12 (effectively acting as a constant).

the #ifdef statement means "if something is defined, then compile this code; if not, then compile the code after #else." It works exactly like the if/else in the code, except that it is executed before the actual compilation (NOT during run time) and it's outcome becomes part of the source code. Hence you can determine which part of your code becomes part of your final executable and which doesn't based on other pre-processor directives.

In the example, if WIN32 is defined (i.e. #define WIN32 was called, which specifies you are compiling on a win32 platform), then your code will look like this:

closesocket(sockfd);
WSACleanup();

if WIN32 is not defined (you are not working on a win32 platform), then your code will look like this:

close(sockfd);


Using #ifdef or just #if is a good way to create code that can be compiled on multiple platforms by specifying which parts of it should be included on which platform.

Another common example is #define DEBUG (or _DEBUG or something) which specifies that you are compiling in debug mode. Then you can encompass your debug code into the #ifdef #endif statements to enable parts of it being included only when debug is compiled, for example:

#ifdef DEBUG
MyLogger.Log("I am logging variable x: " + x);
#endif

Thus, if you define DEBUG, you will log x which is usefull for troubleshooting. For final release, simply do not define DEBUG and all your debug code will simply get excluded, and thus wont bog down your performance and executable size.

(note, many compilers have a debug and release compilation modes which automatically #define a DEBUG, read up on your IDE / compiler about that)..

Google "preprocessor directives" for more info

Share this post


Link to post
Share on other sites
thanks, but what is a difference between the two cases i.e. why on win 32 we have to do2 things in our code closesocket(sockfd);
WSACleanup();,

while in else we have to do only 1 thing.
close(sockfd);

Share this post


Link to post
Share on other sites
Because in Windows you're using WinSock for your sockets API, which has some additional functions and requirements. On non-Windows platforms (presumably *nix) you're using vanilla BSD sockets, where the sockets are closable as file handles.

Share this post


Link to post
Share on other sites

This topic is 3491 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this