Site icon DataFlair

Macros in C Programming – Don’t Consider it as an Outdated Feature

Today, macros in C is considered as outdated in modern programming practices, it still finds applications in C by making things easier for the programmer. We will help you to develop a clear understanding of macros by covering even the minute concepts.

In this tutorial, we will discuss:

1. Macros in C Programming

Macros are nothing but a piece of code based on the #define preprocessor. In the C programming language, a macro would generally look like:

#define MACRO macro_value

Key takeaway: It is important to note that the macros are not terminated by a semicolon (;)

For a better understanding of Macros, it is important to understand preprocessors in C.

2. Types of Macros in C Programming

In C, Macros are broadly classified into two distinct types. They are namely:

2.1 Object-like Macros

It appears to be a symbolic constant. It can be termed as an alternative way to define an identifier used to represent constant expressions. The simplest example would be:

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

#define PI 3.14

2.2 Function-like Macros

It is an expression, used to perform a particular operation. It is an alternative way to define a function. A simple example would be:

#define RECTANGLE(l,b) l*b

Here is a code in C that illustrates the use of macros to find the area of a rectangle:

#include<stdio.h>
#define RECTANGLE(l,b)l*b
int main()
{

printf("Welcome to DataFlair tutorials!\n\n");

int length = 3, breadth = 4;
int area = RECTANGLE(length,breadth);
printf("The area is: %d\n", area);
return 0;
}

Having problems in understanding the above program? Check this out Basic Syntax Rules – Learn the ABC’s of  CProgramming

Code on Screen-

Output-

Types of Predefined Macros in C

Here is a table that summarizes some of the basic macros used in the C programming language:

  1. _DATE_  – Used to represent the current date in MM DD YYYY format.
  2. _STDC_  – Used to indicate when the compiler compiles the code successfully by returning the value 1.
  3. _TIME_  – Represent the current time in HH:MM:SS.
  4. _LINE_  – Represent the current line number.
  5. _FILE_  – Represent the current file name.

Here is a code in C that illustrates the use of macros:

#include<stdio.h>
int main()
{

printf("Welcome to DatafLair tutorials!\n\n");

printf("The current date is: %s\n", __DATE__ );
printf("The current time is: %s\n", __TIME__ );
printf("The total lines in the code is: %d\n", __LINE__ );
printf("The file name is: %s\n", __FILE__ );
printf("STDC would return the value: %d\n", __STDC__ ); // %d is used since it would return an integer value
return 0;
}

Code on Screen-

Output-

Quiz on Macros in C

Time limit: 0

Quiz Summary

0 of 15 Questions completed

Questions:

Information

You have already completed the quiz before. Hence you can not start it again.

Quiz is loading…

You must sign in or sign up to start the quiz.

You must first complete the following:

Results

Quiz complete. Results are being recorded.

Results

0 of 15 Questions answered correctly

Your time:

Time has elapsed

You have reached 0 of 0 point(s), (0)

Earned Point(s): 0 of 0, (0)
0 Essay(s) Pending (Possible Point(s): 0)

Categories

  1. Not categorized 0%
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  1. Current
  2. Review / Skip
  3. Answered
  4. Correct
  5. Incorrect
  1. Question 1 of 15
    1. Question

    #include <stdio.h>

    #define Limit 5

    int main()

    {

    printf(“%d”,Limit);

     

    return 0;

    }

    Correct
    Incorrect
  2. Question 2 of 15
    2. Question

    #include <stdio.h>

    #include <math.h>

    #define Limit 5

    int main()

    {

    int i=pow(2,Limit);

    printf(“%d”,i);

    return 0;

    }

    Correct
    Incorrect
  3. Question 3 of 15
    3. Question

    #include <stdio.h>

    #include <math.h>

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

    int main()

    {

    int i= max(5,2);

    printf(“%d”,i);

    return 0;

    }

    Correct
    Incorrect
  4. Question 4 of 15
    4. Question

    #include <stdio.h>

    #include <math.h>

    #define increment(i) i++

    int main()

    {

    int i=5;

    printf(“%d”,increment(i));

    return 0;

    }

     

    Correct
    Incorrect
  5. Question 5 of 15
    5. Question

    #include <stdio.h>

    #include <math.h>

    #define increment(i) ++i

    int main()

    {

    int i=5;

    printf(“%d”,increment(i));

    return 0;

    }

    Correct
    Incorrect
  6. Question 6 of 15
    6. Question

    #include <stdio.h>

    #include <math.h>

    #define multi(x,y) x*y

    int main()

    {

    printf(“%d”,multi(3+1,9*1));

    return 0;

    }

    Correct
    Incorrect
  7. Question 7 of 15
    7. Question

    #include <stdio.h>

    #include <math.h>

    #define multi(x,y) (x)*(y)

    int main()

    {

    printf(“%d”,multi(3+1,9*1));

    return 0;

    }

    Correct
    Incorrect
  8. Question 8 of 15
    8. Question

    #include <stdio.h>

    #include <string.h>

    #define merge(x,y) (x##y)

    int main()

    {

    printf(“%d”,merge(56,98));

    return 0;

    }

    Correct
    Incorrect
  9. Question 9 of 15
    9. Question

     

    #include <stdio.h>

    #include <string.h>

    #define merge(x,y) (x##y)

    int main()

    {

    printf(“%d”,merge(‘G’,’o’));

    return 0;

    }

    Correct
    Incorrect
  10. Question 10 of 15
    10. Question

    #include <stdio.h>

    #include <string.h>

    #define merge(x,y) (x##y)

    int main()

    {

    char c=’G’;

    char c1=’o’;

    printf(“%d”,merge(c,c1));

    return 0;

    }

    Correct
    Incorrect
  11. Question 11 of 15
    11. Question

     which among the following is a preprocessor directive?

    Correct
    Incorrect
  12. Question 12 of 15
    12. Question

    Which among the following is not true about macro? 

    Correct
    Incorrect
  13. Question 13 of 15
    13. Question

    Which among the following is not a preprocessor directive?

    Correct
    Incorrect
  14. Question 14 of 15
    14. Question

    which of the following is used to undefine a macro in c++

    Correct
    Incorrect
  15. Question 15 of 15
    15. Question

    which among the following is a conditional compilation directive?

    Correct
    Incorrect

Summary

In this tutorial, we discussed the basic meaning of macros, its 2 basic types with appropriate examples. We further carried our discussion on the various types of predefined macros available in the C programming with the help of a program.

It’s the right time to explore Typecasting and Type Conversion in C

We hope you liked the article. Don’t forget to share your experience.

Exit mobile version