Search This Blog

Translate This Page

Friday, 30 August 2013

Multiline macros in C

Multi Line Macro in C


We can write multi line macro same like function but each statement ends with "\". Below is the simple macro , which accepts input number from user, and prints whether entered number is even or odd.

#include <stdio.h>

#define MACRO(num, str) ({\
            printf("%d", num);\
            printf(" is");\
            printf(" %s number", str);\
            printf("\n");\
           })

int main(void)
{
    int num;
    printf ("Enter no\n");
    scanf("%d", &num);

    if (num & 1)
        MACRO(num, "Odd");
    else
        MACRO(num, "Even");

    return 0;
}


Output:

#cc     macro.c   -o macro
#./macro

#Enter a number: 10

#10 is Even number

No comments:

Post a Comment

Latest Current Visitor

World Clock Time, for View Click on Country