position: fixed; top: auto !important; margin-left: 112px;
Copied to clipboard..

Macros in C

In this tutorial, we will explore the following topics of macros in c and the article reading time is almost 3 min 29 sec. So let's get started.

 
    
  • What is C Preprocessor?
  • How to define Macros in c?
  • How to undefine Macros in C?
  • What is Preprocessor?

    C preprocessor is included at the top of programming file which includes all the functions and code written in that given preprocessor directive. You will get all the code written into it with a single magic line in your program which starts with #include.

    For example,

    
    
     #include<stdio.h>
    

    This statement will include all the functions written inside the "header.h" file in your program. It makes the code simple and easy to understand. Preprocessors always starts with # character. printf() and scanf() functions are written in stdio.h file so that you need not to worry about the inner details of those functions.

    Define Macros in C

    To define macros in C programming, #define macro is used. Once it is defined in the program, you can use it anywhere in your program. Syntax to define macros is given below,

        #define MACRO_NAME  MACRO_VALUE
    

    For example,

    
    
     #define PI 3.14
    

    Now, whenever compiler finds PI in the program it will replace PI with define MACRO_VALUE which is 3.14. Let's illustrate this practically in the program of finding an area of circle.

    
    
    #include <stdio.h>
    
    #define PI 3.14
    int main(void) {
        
        float area = PI*2*2;
        
    	printf("%f",area);
    	return 0;
    }
    

    Output

    12.560000

    In this example, we define PI macro with constant value 3.14. As we know that the value of PI will never be changed, we declared it as an Macro in our program. Now, inside a main() function, we calculated the area of circle and the PI macro will be replaced by the value 3.14 by compiler and finally we get an output as 12.56000

    Undefine Macro

    To undefine a macros in c, #undef is used. Undef in c is used to undefine an Macro constant value or function. Syntax to write an #undef macro is given below,

    
    
    #undef MACRO MACRO_VALUE
    

    Example,

    
    
    #undef PI 3.14
    

    How to Redefine a macro?

    To redefine a macro, there is not explicitly any preprocessor directive is available. You have to first undefine a macro and then again define it. Syntax to redefine a macro is given below,

    
    
    #ifdef MACRO
    #undef MACRO
    #endif
    
    #define NEW-MACRO
    

    Example

    
    
    #include <stdio.h>
    
    #define PI 3.14
    int main(void) {
        
        #ifdef PI
        #undef PI
        #endif
        
        #define PI 3.1466
        float area = PI*2*2;
        
    	printf("%f",area);
    	return 0;
    }
    

    Output

    12.586400

    Code is self explanatory. To redefine any macro you will check if Macro is already defined. If it is then undefine it using #undef Macro_Name and then define it again.

    In this program, we first defined a MACRO PI. Then inside main() function we undefined it and redefined again with value 3.1466 and then calculated an area of circle and printed the output.



    Previous Next Article








    codzify.com


    Largest collection of up-to-date tutorials to learn programming languages. We are focused on easy learning. Massive collection of interview questions one may need for preparation.

    Social Profile


    Linkedin
    Twitter
    Facebook

    Copyright 2018. All rights reserved.