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

Macro With Argument v/s Function: C Program

In today’s video tutorial lets see the difference between a Macro with argument and a simple function.

Related Read:
Macros With Arguments: C Program
Function / Methods In C Programming Language

Video Tutorial: Macro With Argument v/s Function Call: C Program


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

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

Source Code: Macro With Argument v/s Function Call: C Program

#include<stdio.h>

#define PI 3.14159265358979323846
#define AREA(r) ( PI * r * r )

double circle_area(float);

int main()
{
    float r;

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

    printf("Area of Circle, using Macro is %f\n", AREA(r));
    printf("Area of Circle, using Function is %f\n", circle_area(r));

    return 0;
}

double circle_area(float r)
{
    return( 3.14159265358979323846 * r * r );
}

Output:
Enter radius of Circle
5
Area of Circle, using Macro is 78.539816
Area of Circle, using Function is 78.539816

As you can see in above output the result is same for Macro and function call. Now the question is, when to use macros and when to use functions?

When to use macros and when to use functions?

Macro: If a macro template is used 100 times inside a C program, all these macro template will be replaced with its macro expansion before its passed to the compiler. This process is called as preprocessing, and it’s done by preprocessor. Because of this, the size of the program increases.

Function: Function definition is written only once and even if its called 100 times inside a program it won’t increase the program size. Whenever there is a function call, the control is passed to the function definition where some logical operation is performed and then a meaningful result is returned back to the calling function. This takes some time.

So the trade off is between memory space and time.

If memory is not an issue, then you can simply use Macros – as it brings in speed of execution as an advantage. But if memory space is an issue – if you’re writing for a mobile device, then it’s better to go with functions. Though its slower, it uses less memory space.

While coding for bigger real-time projects you’ll surely need the knowledge of macros and macros with argument, so know the syntax and it’s advantages. In this fast moving world speed of execution really matters. And C programming specifically is famous and used often for its speed and its easy integration(and interaction) with the hardware devices.

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 Program To Find Area of Circle Using Macro Expansion

In this video tutorial lets learn about Macro Expansion and also lets write a simple program to find area of Circle using Macros.

Related Read:
Preprocessors In C Programming Language

Rules To Construct/Write Macro Definition

1. Space between # and define is optional.
2. There must be a space or tab between Macro Template and Macro Expansion.
3. #define MACRO_TEMPLATE MACRO_EXPANSION is called Macro Definition.
4. Macro definition must not end with semi-colon.
5. Using all capital letters for Macro template is convention and not a syntax of C. You can use any name for Macro template, except the C reserve words.

Video Tutorial: C Program To Find Area of Circle Using Macro Expansion


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

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

Source Code: C Program To Find Area of Circle Using Macro Expansion

#include<stdio.h>

#define PI 3.14

int main()
{
    float r, area;

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

    area = PI * r * r;

    printf("Area of Circle is %f\n", area);

    return 0;
}

Output 1:
Enter Radius of Circle
5
Area of Circle is 78.500000

Source Code: Writing printf statement in Macro expansion

#include<stdio.h>

#define DISPLAY printf("I'm in Love with iPhone UI Elements\n\n")

int main()
{
    DISPLAY;
    return 0;
}

Output 1:
I’m in Love with iPhone UI Elements

Note: Careful not to enter semicolon at the end of Macro definition. i.e., after the end of printf() statement.

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