In this blog, we will learn about macros in c language or c define macro we will have a look at what macros are and why we use these macros in c language, the syntax we require to use them, and how are they compiled during compilation, we will also discuss the types of macros with a detailed explanation of their examples with code and output of the same we will also discuss the advantages of the macros or c defined macro.
What are Macros in C Language?
Suppose a scenario when we are working on a program and there is one value or segment of code or object that keeps repeating if we can define it once and then use just that definition in the code can save a lot of time and space the code and make it more readable. We can achieve the above solution by using macros in c language.
Macro in c language is a segment of code that may be swapped out with a macro value. The macro is defined using the #define preprocessor directive and does not have a semicolon at the end (;). The term "macro" is only a label given to certain values or phrases; it does not designate a place in memory. The macro definition in c
The macro value is substituted for the macro in c language name each time the compiler comes across the macro. The names of the two macros in c language could not coincide.
Syntax of Macros in C Language
In the general syntax for c macro function or macros in c language, we have three main parts that we have to keep in our mind. This will be the general syntax while writing any macro program.
The syntax includes “#define” followed by “macro name” and at last “ the value of macro”.
-
define: it is a preprocessor directive.
- Macro name: This will be the name of the macro that you will use in the code which will use to search for the maco outside the call.
- Macro value: As the name suggests this is the value assigned with a macro which will be used in the code whenever a macro in c language name is mentioned.
Example of Macro in C Language
In this section of the blog, we will discuss the example of macro in c language
Code Implementation:
#include<stdio.h> // This is macro definition #define PI 3.14 void main() { // declaration and initialization of radius int radius = 22; // decalarion and calculating the circumference float area = 2 * PI * radius; //printing the circumference of the circle printf("Circumference of circle is %f", area); }
Output
Circumference of circle is 138.160004
Explanation of the above example
In the above example, we have used a macro in c language named PI and assigned it the value 3.14, and in the code, we are finding the circumference of the circle and we need to use the value of pie in the code we have just named the macro in c that we have mentioned above and in the formulae, we have used the macro name and we are guessing the required answer.
Types of Macro in C Language
There are mainly four types of macros in c language, we will discuss all of them with a proper explanation of their code and output.
-
Object Like Macro in C Language
A simple identifier that will be changed by a piece of code is an object-like macro. Because it resembles an object in code that utilizes it, it is referred to be object-like. It is common practice to substitute a number or variable expressed as a constant for a symbolic name. In simple words, we will replace the identifier with a fragment or part of the code.Example
#include <stdio.h> // Macro definition #define DATE 19 int main() { // Print the message printf("Prepbytes was founded on" " %d-FEB-2019", DATE); return 0; }
OutputPrepbytes was founded on 19-FEB-2019
Explanation of the above example
In the above example, we have used a macro in c language with the name date and simply used it to print the message involving that date. Like we have used the date to print the message with the required date. -
Chain-Like Macros
These are the certain type of c define macro or macros that are considered to be macros as they are macros inside the c define macro or macros in simple words when a macro is defined inside a macro it is known as a chain-like macro. The flow of accessing the c define macros is first the parent macro will be expanded by the compiler after that the child macros which are defined inside the parent macro are accessed or expanded by the compiler.Example
In this example, we will study the example of a chain like macros in c language.Code Implementation:
#include <stdio.h> #define INSTAGRAM FOLLOWERS #define FOLLOWERS 113 // Driver Code int main() { // Print the message printf("Prepbytes have %dK" " followers on Instagram", INSTAGRAM); return 0; }
OutputPrepbytes have 113K followers on Instagram
Explanation of the above example
In the above example, we have studied the chain-like macros in c language. Here we have used two macros one with the name “INSTAGRAM FOLLOWERS” and the other with the name “ FOLLOWERS”. First, INSTAGRAM is developed to generate FOLLOWERS. The extended macro is then expanded to provide the result as 138K. This is referred to as macro chaining. -
Multi-Line Macro
We can define a macro in c language in multiple lines as some c define macro names need to be present in multiple lines so in those cases the multi-line macros are very important
We can just change the line by using a backsplash – newlineCode Implementation:
// C program to illustrate macros #include <stdio.h> // Multi-line Macro definition #define ELEM 21, \ 22, \ 23, \ 24 // Driver Code int main() { int arr[] = { ELEM }; printf("Elements of Array are:\n"); for (int i = 0; i < 4; i++) { printf("%d ", arr[i]); } return 0; }
OutputElements of Array are: 21 22 23 24
Explanation of the above code
In the above code, we have used multi-line macros and in that multi-line macros, we have declared elements of the array each element is declared in the new line and the name of the macro is elem. -
Function Like Macro
In C programming, function-like macros are remarkably similar to actual functions.
The macro name can be used to supply parameters and carry out operations in a code segment.
Since there is no type checking for arguments in c define macros, it is advantageous to send several datatypes through the same macro in the C programming language.Example
In this example, we will understand the function like a macro with proper code and output and will have the proper explanation of the same.Code Implementation:
// C program to illustrate macros #include <stdio.h> // Function-like Macro definition #define max(a, b) (((a) < (b)) ? (b) : (a)) // Driver Code int main() { // Given two number a and b int a = 35; int b = 22; printf("Maximum value between" " %d and %d is %d\n", a, b, max(a, b)); return 0; }
OutputMaximum value between 35 and 22 is 35
Explanation of the above code
In the above code we have used a function like a macro in c language here we have tried to find the maximum of two numbers by using ternary operators and we have found out the max of two numbers using that macro in c language.
Predefined Macros in C language
While Writing any macro program you can use these predefined macros in c language.
S.No | Macro | Feature |
---|---|---|
1. | FILE _ Macro | The file where the program is stored is contained here |
2. | _TIME _ Macro | It will contain the time in HH::MM format |
3. | _LINE _ Macro | It will store the current line number where the macro is used in the code. |
4. | DATEMacro | It contains the current date in MM: DD::YYYY format. |
5. | STDCMacro | It will result in one when there is a successful compilation. |
Advantages of Macros in C
- Macros in C language will enhance the readability of the code by making use of meaningful values and operations.
- Macros in c language will increase the code execution speed at the preprocessing stage instead of at runtime.
- By using c define macro or macros in c language we can perform certain operations that are not possible in normal language.
Disadvantages of Macros in C
- Macros in C have some extra error handling which will lead to unexpected behavior.
- We cannot write macros in c with the same name.
- Sometimes the expression can be evaluated multiple times in macros in c.
Conclusion
In this article, we have studied macros in c language with a brief introduction followed by a detailed explanation and the case where macros in c are used, after that, we have discussed the types of macros in c language with an explanation of each type followed by their example code and explanation. At last, we have discussed the advantages and disadvantages of macros in c.