![]() ![]() We defined the PI constant as a macro with the value 3.14. In the above code example, we wrote a program to calculate the area of a circle with the help of a given radius. We will take a deeper look at what preprocessor directive is in the later part of this article, but first, let's try to understand macro with the help of code example- Code: The syntax of the macro is as shown in the following figure. Whenever the compiler encounters the macro, it replaces the macro name with the macro value. Macro is just a name given to certain values or expressions it doesn't point to any memory location. The macro is defined with the help of #define preprocessor directive and the macro doesn’t end with a semicolon( ). The macro in C language is known as the piece of code which can be replaced by the macro value. In this article, we will explore the use of macros in C language, their different types and use cases. Macros are one of the convenient ways to write robust and scalable code. Suppose we are working on an application in C language and there is one value or an object or segment of code which we require so many times in our code then with the help of macros we can define it once and use it many times. This article contains a list of predefined macros in C language with examples and explanations.This article will introduce you to the different types of macros used by programmers.You will get introduced to the work of the preprocessor and the use of the #define directive.This article will help you to understand the basic concept of macro with several examples.Macros have multiple types and there are some predefined macros as well. Macros in C are very useful at multiple places to replace the piece of code with a single value of the macro. We got what we wanted, neighbor IP worked out automatically from IP assigned to local interface.īut, the longer I look at it the more I don't like feel of this logic and manipulation preceding the actual neighbor statements.Macro in C programming is known as the piece of code defined with the help of the #define directive. Īnd this is the result of rendering: router bgp 65001 Now you have only one place where the banner needs to be changed and anything else using this macro will reflect the changes automatically. Not only will you reduce mistakes that can happen during copying but you also make future updates to the banner much easier. Instead of copy pasting text of the banner across your templates you can create macro and have it render the banner. Take for example device banners, these tend to be static but are used over and over again. You might benefit from macro even when all it does is rendering static text. Macros are great for creating reusable components when we find ourselves copy pasting around same lines of text and code. You can essentially treat macro as one big evaluation statement that also allows parametrization. Inside of macros we can use any of the Jinja features and constructs. Macros can take arguments or be used without them. We use them to encapsulate logic used to perform repeatable actions. Macros are similar to functions in many programming languages. GitHub repository with resources for this post.Jinja2 Tutorial - Part 6 - Include and Import.Jinja2 Tutorial - Part 4 - Template filters.Jinja2 Tutorial - Part 3 - Whitespace control.Jinja2 Tutorial - Part 2 - Loops and conditionals.Jinja2 Tutorial - Part 1 - Introduction and variable substitution. ![]() We'll talk about what macros are, why we would use them and we'll see some examples to help us appreciate this feature better. Welcome to the part 5 of Jinja2 Tutorial where we learn all about macros.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |