Macro Continuation (\) Preprocessor Operator: C Program

In this video lets see how we can have multiple line of code inside macro expansion, by using preprocessor operator – macro continuation( \ ).

Where Is It Used?

While you’re writing complex logic inside macro expansion, you’ll need to break the line and write code in next line. In such cases macro continuation operator is very helpful. And the code looks much cleaner and clearer.

Video Tutorial: Macro Continuation (\) Preprocessor Operator: C Program


[youtube https://www.youtube.com/watch?v=WUcSe_pSv0o]

YouTube Link: https://www.youtube.com/watch?v=WUcSe_pSv0o [Watch the Video In Full Screen.]

Source Code: Macro Continuation (\): C Program

#include<stdio.h>

#define SQUR(x) \
        printf("Square of %d is %d\n", x, (x * x));

int main()
{
    SQUR(5);

    return 0;
}

Output:
Square of 5 is 25

Here we are writing the macro expansion in the next line, so we are making use of macro continuation preprocessor operator (\).

Source Code: Macro Continuation (\) Preprocessor Operator: C Program

#include<stdio.h>

#define COMPANY(x) switch(x) { \
                     case 1: printf("1. Oracle\n"); break; \
                     case 2: printf("2. IBM\n"); break; \
                     case 3: printf("3. Ripple\n"); break; \
                     default: printf("default. Banks\n"); \
                   }

int main()
{
    COMPANY(3);
    COMPANY(2);
    COMPANY(50);

    return 0;
}

Output:
3. Ripple
2. IBM
default. Banks

Here we’ve multiple lines of code inside macro expansion. So at the end of each line we’ve written macro continuation symbol ( \ – backslash). Wherever you write the macro template, preprocessor will replace it with the macro expansion before execution.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Preprocessor Operator: defined

In this video tutorial lets see how to use preprocessor operator “defined” in C programming language.

How Does ‘defined’ preprocessor operator work?

defined preprocessor operator can only be used with #if and #elif directives.

If the Macro name passed to define is actually defined, then it returns true or else it’ll return false.

Syntac of “defined”

#if( defined(macro_name1) )
  // Some Set of Code
#elif( defined(macro_name2) )
  // Some More Set of Code
#endif

OR

#if defined(macro_name1) 
  // Some Set of Code
#elif defined(macro_name2) 
  // Some More Set of Code
#endif

Video Tutorial: C Preprocessor Operator: defined


[youtube https://www.youtube.com/watch?v=IDiV22rMPBk]

YouTube Link: https://www.youtube.com/watch?v=IDiV22rMPBk [Watch the Video In Full Screen.]

Source Code: C Preprocessor Operator: defined

#include<stdio.h>

#define PI 3.14159265358979323846

#define AREA

void area_circle(float r);
void circumference_circle(float r);

int main()
{
    float r;

    printf("Enter radius of Circle\n");
    scanf("%f", &r);

    #if defined(AREA)
        area_circle(r);
    #elif defined(CIRCUM)
        circumference_circle(r);
    #else
        printf("Code To Be Implemented\n");

    #endif // defined

    return 0;
}

void area_circle(float r)
{
    printf("Area of Circle is %f\n", (PI * r * r) );
}

void circumference_circle(float r)
{
    printf("Circumference of Circle is %f\n", (2 * PI * r) );
}

Output:
Enter radius of Circle
5
Area of Circle is 78.539816

In above source code AREA macro template or macro name is defined. So defined(AREA) returns true in #if condition. Hence the code inside #if block gets compiled and executed.

#include<stdio.h>

#define PI 3.14159265358979323846

#define CIRCUM

void area_circle(float r);
void circumference_circle(float r);

int main()
{
    float r;

    printf("Enter radius of Circle\n");
    scanf("%f", &r);

    #if defined(AREA)
        area_circle(r);
    #elif defined(CIRCUM)
        circumference_circle(r);
    #else
        printf("Code To Be Implemented\n");

    #endif // defined

    return 0;
}

void area_circle(float r)
{
    printf("Area of Circle is %f\n", (PI * r * r) );
}

void circumference_circle(float r)
{
    printf("Circumference of Circle is %f\n", (2 * PI * r) );
}

Output:
Enter radius of Circle
5
Circumference of Circle is 31.415927

Here macro name AREA is not defined, hence code inside #if gets skipped. Whereas macro CIRCUM is defined, so the code inside #elif gets compiled and executed.

If neither AREA and nor CIRCUM is defined, then code inside #else block gets compiled and executed.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert