Conditional Compilation in C

C provides a very useful and interesting feature called “conditional Compilation

the general structure for doing it:

#ifdef macroname

statement 1;
statement 2;
statement 3;
statement 4;
#endif

here if macroname has been #defined then the 4 statements will be compiled otherwise not.

There are 3 scenarios where I see application of this feature:

-> To “comment out” obselete lines of code. It may happen that a program undergoes a change for some reasons.
Here we may not want to delete the old code but add new code.
In this scenario, we can include the old and new code in ifdef block and control the compilation using a single #define statement.

eg:

void main()
{

#ifdef OLD
statement 1;
statement 2;
#else
statement 3;
statement 4;

#endif

}

here , if we define thee macro “OLD” then old code is compiled otherwise, the new code is compiled.

->The other (more sophisticated) use of this feature can be to make the programs more portable.
Thus, we can make use of this feature and make a program work on two completely different computers.

eg:

void main()
{
#ifdef INTEL
code for intel PC
#else
code for motorola PC
#endif
code common for both.

}

This code is pretty self explanatory.
If you want your code to run on INTEL we just need to define a macro.

->A third possible scenario is defining of custom functions.
Lets say we define a function called “my_sample_function()” in a file “file1.h”
Also, “file1.h” is included in “file2.h

If we include both the files, compiler will throw an error saying “multiple declaration of my_sample_function()

To overcome this problem, we can use the following way:

/* file1.h */

#ifndef _file
#define _file
my_sample_function()
{
/*some code */
}
#endif

Here when file1.h gets included first time, compiler knows that macro _file is not defined. Thus it gets defined and the rest of code is compiled.
Next time, since the macro stands defines, the function does not get compiled and thus there is no error.

NOTE: #ifndef is exactly opposite of #ifdef

 

Conditional compilation can also be achieved using #if ,#else #endif statements.

#if is used to evaluate whether a expression evaluated to nonzero value or not.

it is used in the same way as above:

eg:

 

void main(){

if TEST<=5

statment 1;

#else

statement 2;

#endif

}

Adding Functions to Library in C

This might be known to many but  I recently tried it and got it working.

Suppose you make a function in C(for example here we take a simple function which calculates the square of a number).

Its a fairly simple one and I want to add in in a library thus making its reuse possible.

(Please note I am taking a very simple scenario because the actual program is not the point of focus here, but the procedure is)

The function:

int square(int n)

{

return n*n;

}

Compile the file:

Compile the file in which you have written this function. Lets call it “sqr.c”. A new file called “sqr.obj” is created contained compiled code.

Add the function to Library:

Add the function to the library “maths.lib” using the following command.

C:\>tlib maths.lib + c:\sqr.obj

Please note the paths. Your paths may be different than mine!

here, maths.lib is the library and + is a switch which indicates that we want to add a new function to the library and the object file is at the given path.

Header file:

Add the prototype of square() function in a header file (lets say “square.h”) This file should be included in the program where we want to use the function. we can use it as:

#include “c:\square.h”

Thats all !

Use the function and enjoy !!

Hope it helps.