{"id":3274,"date":"2024-03-05T12:48:01","date_gmt":"2024-03-05T12:48:01","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=3274"},"modified":"2024-03-05T12:48:01","modified_gmt":"2024-03-05T12:48:01","slug":"macros-and-its-types-in-c-c","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/macros-and-its-types-in-c-c\/","title":{"rendered":"Macros And Its Types In C\/C++"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#macros-and-its-types-in-c-c\">Macros And Its Types In C\/C++<\/a><\/li><li ><a href=\"#programs-to-illustrate-the-use-of-macros-in-c-c\">Programs &#8211; To illustrate the use of macros in C\/C++<\/a><\/li><li ><a href=\"#program-2\">Program 2<\/a><\/li><li ><a href=\"#faq-macros-and-its-types-in-c-c\">FAQ- Macros And Its Types In C\/C++<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"macros-and-its-types-in-c-c\">Macros And Its Types In C\/C++<\/h2>\n\n\n\n<p>A macro in a program is a piece of code that is replaced by its defined value. It is defined using the #define directive in C and C++. When the compiler encounters the name of a macro, it replaces that name with the macro&#8217;s defined value. Unlike regular C statements, macro definitions do not need to be terminated by a semicolon (<code>;<\/code>). Macros are used for code replacement and are often employed for defining constants, inline functions, and conditional compilation directives in C and C++ programs.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"programs-to-illustrate-the-use-of-macros-in-c-c\">Programs &#8211; To illustrate the use of macros in C\/C++<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\n\/\/ C program to illustrate macros\n#include &lt;stdio.h&gt;\n \n\/\/ Macro definition\n#define LIMIT 5\n \n\/\/ Driver Code\nint main()\n{\n    \/\/ Print the value of macro defined\n    printf(\"The value of LIMIT\"\n           \" is %d\",\n           LIMIT);\n \n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>The value of LIMIT is 5\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"program-2\">Program 2<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\n\/\/ C program to illustrate macros\n#include &lt;stdio.h&gt;\n \n\/\/ Macro definition\n#define AREA(l, b) (l * b)\n \n\/\/ Driver Code\nint main()\n{\n    \/\/ Given lengths l1 and l2\n    int l1 = 10, l2 = 5, area;\n \n    \/\/ Find the area using macros\n    area = AREA(l1, l2);\n \n    \/\/ Print the area\n    printf(\"Area of rectangle\"\n           \" is: %d\",\n           area);\n \n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Area of rectangle is: 50\n<\/code><\/pre>\n\n\n\n<p>Explanation<\/p>\n\n\n\n<p> In this case, when the compiler encounters <code>AREA(l, b)<\/code> in the program, it replaces it with the macro&#8217;s definition, which is <code>(l*b)<\/code>. So, if you use <code>AREA(10, 5)<\/code> in your code, it will be equal to <code>10*5<\/code>. Macros provide a way to perform simple text replacement in your code, which can be helpful for defining constants or creating code templates for repetitive tasks.<\/p>\n\n\n\n<p><strong>Type Of Macros<\/strong><\/p>\n\n\n\n<p> Object-like macros are simple identifiers that are replaced with code fragments. They are called &#8220;object-like&#8221; because they resemble objects in the code where they are used. These macros are commonly used to replace symbolic names with constant values or variables, providing a convenient way to make code more readable and maintainable.<\/p>\n\n\n\n<p><strong>Illustration of Simple Macro<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C program to illustrate macros\n#include &lt;stdio.h&gt;\n \n\/\/ Macro definition\n#define DATE 31\n \n\/\/ Driver Code\nint main()\n{\n    \/\/ Print the message\n    printf(\"Lockdown will be extended\"\n           \" upto %d-MAY-2020\",\n           DATE);\n \n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Lockdown will be extended upto 31-MAY-2020\n<\/code><\/pre>\n\n\n\n<p>2. Chain Macros: Macros inside macros are referred to as  Chain Macros. Initially, the Parent Macro is being expanded and then the child macro is expanded.<\/p>\n\n\n\n<p><strong>The illustration of child macro is given below<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\n\/\/ C program to illustrate macros\n#include &lt;stdio.h&gt;\n  \n\/\/ Macro definition\n#define INSTAGRAM FOLLOWERS\n#define FOLLOWERS 138\n  \n\/\/ Driver Code\nint main()\n{\n    \/\/ Print the message\n    printf(\"Skill Vertex  have %dK\"\n           \" followers on Instagram\",\n           INSTAGRAM);\n  \n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Skill Vertex have 138K followers on Instagram\n<\/code><\/pre>\n\n\n\n<p>This is called &#8220;chaining of macros.&#8221; For example, &#8220;INSTAGRAM&#8221; expands to &#8220;FOLLOWERS,&#8221; and then &#8220;FOLLOWERS&#8221; further expands to &#8220;138K.&#8221;<\/p>\n\n\n\n<p>3. Multi-line Macros<\/p>\n\n\n\n<p>An object-like macro will contain multi-line. Hence, you need to require a backslash-newline for creating a multi-line macro.<\/p>\n\n\n\n<p>The illustration of multiline macros are given below<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C program to illustrate macros\n#include &lt;stdio.h&gt;\n \n\/\/ Multi-line Macro definition\n#define ELE 1, \\\n            2, \\\n            3\n \n\/\/ Driver Code\nint main()\n{\n \n    \/\/ Array arr&#91;] with elements\n    \/\/ defined in macros\n    int arr&#91;] = { ELE };\n \n    \/\/ Print elements\n    printf(\"Elements of Array are:\\n\");\n \n    for (int i = 0; i &lt; 3; i++) {\n        printf(\"%d  \", arr&#91;i]);\n    }\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Elements of Array are:\n1  2  3<\/code><\/pre>\n\n\n\n<p><strong>4. Function-like Macro: <\/strong><\/p>\n\n\n\n<p>Function-like macros in C are similar to function calls and replace code with a set of instructions. To use them, you need to include a pair of parentheses immediately after the macro name. If you add a space between the macro name and the parentheses in the definition, the macro won&#8217;t work.<\/p>\n\n\n\n<p>Function-like macros are expanded only when their name is followed by a pair of parentheses. If you omit the parentheses, it can lead to a syntax error because the compiler may interpret it as a function pointer getting the address of the real function.<\/p>\n\n\n\n<p>The illustration of function-like macros is given below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C program to illustrate macros\n#include &lt;stdio.h&gt;\n \n\/\/ Function-like Macro definition\n#define min(a, b) (((a) &lt; (b)) ? (a) : (b))\n \n\/\/ Driver Code\nint main()\n{\n \n    \/\/ Given two number a and b\n    int a = 18;\n    int b = 76;\n \n    printf(\"Minimum value between\"\n           \" %d and %d is %d\\n\",\n           a, b, min(a, b));\n \n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Minimum value between 18 and 76 is 18\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"faq-macros-and-its-types-in-c-c\">FAQ- Macros And Its Types In C\/C++<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1699271285278\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. What is macro and its types?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. There are two types of macros:<br \/><strong>Executive Macros:<\/strong> These macros generate code or data that becomes part of the assembled program. They typically produce executable instructions.<br \/><strong>Declarative Macros:<\/strong> These macros produce information that aids the assembly process in generating code. They provide data or directives that guide the assembly process.<br \/>These two types of macros serve different purposes in the context of macro processing and code generation during assembly.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1699271294124\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. What is a macro type in C++?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. In C++, macros are code snippets that can be replaced by specific values. There are different types: object-like (simple identifiers), function-like (similar to functions), multi-line (span multiple lines), and chain macros (macros within macros, allowing nesting). They simplify code in C++ programming.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1699271303104\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. Why use macros in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans.In C programming, macros are used to reuse values or code throughout a program. They are defined once and can be employed repeatedly. Additionally, C offers predefined macros.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Macros And Its Types In C\/C++ A macro in a program is a piece of code that is replaced by its defined value. It is defined using the #define directive in C and C++. When the compiler encounters the name of a macro, it replaces that name with the macro&#8217;s defined value. Unlike regular C &#8230; <a title=\"Macros And Its Types In C\/C++\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/macros-and-its-types-in-c-c\/\" aria-label=\"More on Macros And Its Types In C\/C++\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":5408,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[545],"class_list":["post-3274","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-macros-and-its-types-in-c-c","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/3274","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=3274"}],"version-history":[{"count":8,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/3274\/revisions"}],"predecessor-version":[{"id":7917,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/3274\/revisions\/7917"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/5408"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=3274"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=3274"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=3274"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}