Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • MACROS IN C

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 508
    Comment on it

    MACROS

    A macro is a piece of statements(code) which we provide a name or give that piece of statements a name. so whenever we used that name which we provide to that piece of code in the program, that code get replaced by the contents of the macro.

    Advantages of using macros.

    • Macro effect the speed of execution of a program. It increase the speed of a execution of a program

    -It saves a lot of time.

    • It make program shorter in length.

    TYPES OF MACROS

    • Object-like macros

    • function-like macros

      Object-like macros do no havet parameters, whereas function like have parameters.

    **SYNTAX OF MACROS**

    #define identifier(identifier 1,identifier 2,identifier 3.....identifier n) token-string
    

    #define is used for macro defination. in above syntax of macros contain the token list part, this part is a optional part but it used mainly in every case.

    example:- #define area(a) (3.14*(a)*(a))

    Here, the argument passed is a. Every time the program encounters area(argument), it will be replace by perimeter(3.14*(argument)*(argument)).

    C Program to find area of a circle by macros.

     #include <stdio.h>
        #define Pie 3.1415                       // pie macros
        #define area(a) (Pie*(a)*(a))      // area macros
        int main()
    {
            int r;                    
            float area;
            printf("Enter the radius: ");
            scanf("%d",&r);
            area=area(r);
            printf(" Area is=%f",area);
            return 0;
        }
    

    OUTPUT

    Enter the radius: 4 
    Area is=50.264
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: