#if




#if <value>
/* code to execute if this value is true */
#elsif lt;value>
/* code to execute if this value is true */
#else
/* code to execute otherwise
#endif


#if checks whether the value is true (in the C and C++ sense of everything but 0) and if so, includes the code until the closing #endif. If not, that code is removed from the copy of the file given to the compiler prior to compilation (but it has no effect on the original source code file). There may be nested #if statements. It is common to comment out a block of code using the following construction because you cannot nest multi-line comments in C or C++.

#if 0
/* code */
#endif


http://www.cprogramming.com/reference/preprocessor/if.html


#ifndef



#ifndef <token>

/* code */
#else
/* code to include if the token is defined */
#endif


#ifndef checks whether the given token has been #defined earlier in the file or in an included file; if not, it includes the code between it and the closing #else or, if no #else is present, #endif statement. #ifndef is often used to make header files idempotent by defining a token once the file has been included and checking that the token was not set at the top of that file.


#ifndef _INCL_GUARD
#define _INCL_GUARD
#endif


http://www.cprogramming.com/reference/preprocessor/ifndef.html


typedef struct {
int data;
int text;
} S1;

// This will define a typedef for S1, in both C and in C++

struct S2 {
int data;
int text;
};

// This will define a typedef for S2 ONLY in C++, will create error in C.

struct {
int data;
int text;
} S3;
// This will declare S3 to be a variable of type struct.
// This is VERY different from the above two.
// This does not define a type. The above two defined type S1 and S2.
// This tells compiler to allocate memory for variable S3

void main()
{
S1 mine1; // this will work, S1 is a typedef.
S2 mine2; // this will work, S2 is also a typedef.
S3 mine3; // this will NOT work, S3 is not a typedef.

S1.data = 5; // Will give error because S1 is only a typedef.
S2.data = 5; // Will also give error because S1 is only a typedef.
S3.data = 5; // This will work, because S3 is a variable.
}
// That's how they different stuff are handy.

also !!!!!!!!!!! This next bit is important for people who using linked
lists etc.

struct S6 {
S6* ptr;
};
// This works, in C++ only.

typedef struct {
S7* ptr;
} S7;
// Although you would think this does the same thing in C OR C++....
// This DOES NOT work in C nor C++ !!!

Comments

Popular posts from this blog

Bounding Box in PIL (Python Image Library)

Dictionary vs KeyValuePair vs Struct - C#

Rendering order and Z-sorting