×

C Tutorial

C Basics

C Data Types

C Input/Output

C Operators

C Conditional Statements

C Control Statements

C Strings

C Functions

C Arrays

C Structure and Unions

C Pointers

C Preprocessor Directives

C Command-line Arguments

C File Handlings

C Graphics

C Advance Topics

C Tips and Tricks

C Important Topics

C Practice

C Language Preprocessor Directives

Preprocessor enables pre-processing the source code before sending the code to compiler for compilation. There are some predefined commands (just like keywords but they are not keywords) which tells to the preprocessor to do some specific task at the time of pre processing of the source code. These commands are known as preprocessor directives.

There are following type of preprocessor
  1. Line Control Directives [__LINE__, __DATE__, __TIME__, __FILE__]
  2. File Inclusion Directives [#include]
  3. Macro Expansion Directives [#define, #undef]
  4. Conditional Compilation Directives [#if, #else, #elif, #ifdef, #ifndef, #endif]


Line Control Directives [__LINE__, __DATE__, __TIME__, __FILE__]

There are following commands to enable to get some specific features in your source code.

Command Description
__LINE__ Returns line number of the source code in integer format.
__DATE__ Returns the current system date in "MMM DD YYYY" format as a character array (string).
__TIME__ Returns the current system date in "HH:MM:SS" format as a character array (string).
__FILE__ Returns the file name as a character array (string).


Consider the example
    #include<stdio.h>
    int main()
    {
	    printf("\nCurrent System Date is : %s",__DATE__);
	    printf("\nCurrent System  TIME is : %s",__TIME__);
	    printf("\nSource File Name is  : %s",__FILE__);
	    printf("\nCurrent Line Number is : %d",__LINE__);
        return 0;
    }
    
Output

    Current System Date is : May 13 2015
    Current System TIME is : 22:42:20
    Source File Name is  : d:\programs\c-language\prg1.cpp
    Current Line Number is : 23
    


File Inclusion Directives

#include command is used to include header file into the source code whether it is standard library or user defined header file.

#include<header-file-name.h> - type command searches the header file into standard library header file, where all standard library header files exits.

#include"header-file-name.h" - type command searches the header file into the current directory then standard directory.


    #include<stdio.h>     // standard header file
    #include<string.h>    // standard header file
    #include<math.h>      // standard header file
    #include"myusb.h"     // user defined (customize) header file

    int main()
    {
	    statements;
        return 0;
    }
    


Macro Expansion Directives



Complex Macro creation with Arguments



Un defining macro using #undef



Conditional Compilation Directives

These directives are used to enable macro expansion based on condition. This technique is used when you wan to execute one source code (set of statements) from multiple choices.

#ifdef and #ifndef

This technique is generally used when you want to define a macro, if macro is not defined previously in the program.

  • #ifdef - Returns true is given Macro-Name is defined.
  • #ifndef - Return true is given Macro-Name is not defined.

Consider the examples

    #include<stdio.h>

    #define MAX	100
 
    int main()
    {
	    /*redefine MAX Macro*/
	    #ifdef	MAX
		    #undef	MAX
		    #define	MAX	200
	    #endif
	    printf("\nMAX : %d",MAX);
        return 0;
    }
    

    MAX : 200
    
    #include<stdio.h>

    int main()
    {
	    /*defining MAX Macro, if not defined*/
	    #ifndef	MAX
		    #define	MAX	200
	    #endif
	    printf("\nMAX : %d",MAX);
        return 0;
    }
    

    MAX : 200
    


#if, #elif, #else and #endif

This technique is used when you have multiple choices and want to execute once of them, it is similar to if else condition (conditional statements).

Syntax

    #if	<condition-1>
	    statement-1;
	    statement-2;
	    …;
    #elif <condition-2>
	    statement-1;
	    statement-2;
	    …;
    …
    #else
	    statement-1;
	    statement-2;
	    …;
    #endif
    


Consider the example

    #include<stdio.h>
    
    #define VALUE   2

    int main()
    {
        #if		(VALUE==1)
	        printf("Value is : 1");
        #elif	(VALUE==2)
	        printf("Value is : 2");
        #elif	(VALUE==3)
	        printf("Value is : 3");
        #else
	        printf("Other Value");
        #endif
        return 0;
    }
    

    Value is : 2
    



Copyright © 2024 www.includehelp.com. All rights reserved.