C Programming language

adplus-dvertising
File inclusion in C
Previous Home Next

File inclusion

The second pre-processor directive we’ll explore in this chapter is file inclusion. This directive causes one file to be included in another. The pre-processor command for file inclusion looks like this:

#include "filename"

If we have a very large program, the code is best divided into several different files, each containing a set of related functions. It is a good programming practice to keep different sections of a large program separate. These files are #included at the beginning of main program file.

Conditional compilation

We can, if we want, have the compiler skip over part of a source code by inserting the preprocessing commands #ifdef and #endif, which have the general form:

#ifdef macroname
statement 1 ;
statement 2 ;
statement 3 ;
#endif

If macroname has been #defined, the block of code will be processed as usual; otherwise not.

A simple example of #if directive is shown below:

main( )
{
#if TEST <= 5
statement 1 ;
statement 2 ;
statement 3 ;
#else
statement 4 ;
statement 5 ;
statement 6 ;
#endif
}

If the expression, TEST <= 5 evaluates to true then statements 1, 2 and 3 are compiled otherwise statements 4, 5 and 6 are compiled. In place of the expression TEST <= 5 other expressions like ( LEVEL == HIGH || LEVEL == LOW ) or ADAPTER == CGA can also be used.

program using #elif directive can be rewritten as shown below:

#if ADAPTER == VGA
code for video graphics array
#elif ADAPTER == SVGA
code for super video graphics array
#else
code for extended graphics adapter
#endif

Miscellaneous directives

There are two more preprocessor directives available, though they are not very commonly used. They are:

  1. #undef
  2. #pragma

#Undef

On some occasions it may be desirable to cause a defined name to become ‘undefined’. This can be accomplished by means of the #undef directive. In order to undefine a macro that has been earlier #defined, the directive, #undef macro template can be used. Thus the statement, #undef PENTIUM would cause the definition of PENTIUM to be removed from the system. All subsequent #ifdef PENTIUM statements would evaluate to false.

#Pragma

This directive is another special-purpose directive that you can use to turn on or off certain features. Pragmas vary from one compiler to another. There are certain pragmas available with Microsoft C compiler that deal with formatting source listings and placing comments in the object file generated by the compiler.

#pragma startup and #pragma exit

These directives allow us to specify functions that are called upon program startup (before main( )) or program exit (just before the program terminates). Their usage is as follows:

void fun1( ) ;
      
void fun2( ) ;
#pragma startup fun1
#pragma exit fun2
main( )
{
printf ( "\nInside maim" ) ;
}
void fun1( )
{
printf ( "\nInside fun1" ) ;
      
}
     
void fun2( )
     
{
printf ( "\nInside fun2" ) ;
      
}

And here is the output of the program.
Inside fun1
Inside main
Inside fun2

Note that the functions fun1( ) and fun2( ) should neither receive nor return any value. If we want two functions to get executed at startup then their pragmas should be defined in the reverse order in which you want to get them called.

#pragma warn

This directive tells the compiler whether or not we want to suppress a specific warning. Usage of this pragma is shown below.

#pragma warn –rvl /* return value */
#pragma warn –par /* parameter not used */
#pragma warn –rch /* unreachable code */
int f1( )
{
int a = 5 ;
}
void f2 ( int x )
{
printf ( "\nInside f2" ) ;
}
int f3( )
{
int x = 6 ;
return x ;
x++ ;
}
void main( )
{
f1( ) ;
f2 ( 7 ) ;
f3( ) ;
}

If you go through the program you can notice three problems immediately. These are:

  1. Though promised, f1( ) doesn’t return a value.
  2. The parameter x that is passed to f2( ) is not being used anywhere in f2( ).
  3. The control can never reach x++ in f3( ).

If we compile the program we should expect warnings indicating the above problems.

Previous Home Next