Macros With Arguments: C Program

In this video tutorial lets see how to write macros with arguments. We’ll illustrate finding biggest of 2 numbers by using “Macros with arguments” concept.

Note: Do not leave space between macro template name and the argument list / parenthesis. Also make sure to write the macro expansion inside parenthesis.

Related Read:
Preprocessors In C Programming Language
C Program To Find Area of Circle Using Macro Expansion

Video Tutorial: Macros With Arguments: C Program (Biggest of 2 Numbers)


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

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

Source Code: Macros With Arguments: C Program

#include<stdio.h>

#define MAX(x, y) ( x > y ? x : y )

int main()
{
    int a, b;

    printf("Enter 2 numbers\n");
    scanf("%d%d", &a, &b);

    printf("Biggest of %d and %d is %d\n", a, b, MAX(a, b));

    return 0;
}

Output 1:
Enter 2 numbers
14
5
Biggest of 14 and 5 is 14

Output 2:
Enter 2 numbers
5
14
Biggest of 5 and 14 is 14

Functions return result after evaluating the expression, but Macros are different. Before compiler compiles the program, preprocessor replaces all the occurrence of Macro template with Macro expansion. So in macros, it won’t return the result, but instead the entire macro expansion gets placed in place of macro template.

So in above C program, printf() becomes:

printf("Biggest of %d and %d is %d\n", a, b, ( a > b ? a : b ));

Related Read:
Biggest of Two Numbers Using Ternary Operator: C

Check Below 2 Source code and it’s output

No Parenthesis around macro expansion

#include<stdio.h>

#define SQUARE(n) n * n

int main()
{
  printf("%f", 64/SQUARE(4));

  return 0;
}

Output:
64

Parenthesis around macro expansion

#include<stdio.h>

#define SQUARE(n) (n * n)

int main()
{
  printf("%f", 64/SQUARE(4));

  return 0;
}

Output:
4

In C programming and in mathematics in general, each arithmetic operation has different precedence. Hence by using parenthesis around macro expansion, it gets the highest precedence.

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

Preprocessors In C Programming Language

In this video tutorial we’ll cover some basics about preprocessors and give you overview of what we’ll be covering in upcoming video tutorials.

Note

1. Preprocessor is a program that processes our source code before it is passed to the compiler. Hence its name pre-processor.

2. Preprocessor Commands are often called as DIRECTIVES.

3. Preprocessor Directives begin with # symbol.

Coming soon ..

1. Macro expansion.
2. File inclusion.
3. Conditional compilation.
4. Miscellaneous directives.
5. We’ll also be covering about predefined macros.

Previously we had covered a little bit about File inclusion, you can take a look at it here include directive in C Program

Video Tutorial: Preprocessors In C Programming Language


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

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

Computer Science Students and Professionals

If you’re a computer science student enrolled in University, your syllabus may not include in-depth learning of preprocessors. But when it comes to competitive exams and real-time application programming you’ll have to use preprocessors a lot. So better learn it now.

In upcoming videos we’ll cover preprocessor concepts in detail with simple example programs to explain the concepts/topics.

So stay tuned, stay subscribed to our YouTube channel and blog.

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