{"id":3318,"date":"2024-03-05T12:55:30","date_gmt":"2024-03-05T12:55:30","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=3318"},"modified":"2024-03-05T12:55:30","modified_gmt":"2024-03-05T12:55:30","slug":"multiline-macros-in-c","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/multiline-macros-in-c\/","title":{"rendered":"Multiline Macros In 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=\"#multiline-macros-in-c\">Multiline Macros In C<\/a><\/li><li ><a href=\"#example-to-illustrate-macro-ends-with-the-semicolon\">An example-to-defined macro is with the semicolon<\/a><\/li><li ><a href=\"#example-to-illustrate-modified-macro\">Example- to illustrate Modified macro<\/a><\/li><li ><a href=\"#faq-multiline-macros-in-c\">FAQ- Multiline macros in C<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"multiline-macros-in-c\">Multiline Macros In C<\/h2>\n\n\n\n<p>A multi-line macro allows you to create a more complex and reusable code block. In C\/C++, you can define a multi-line macro by using the backslash (<code>\\<\/code>) at the end of each line to continue the macro definition on the next line. Here&#8217;s an example of a multi-line macro that checks whether a number is even or odd.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n#include &lt;stdio.h&gt; \n  \n#define MACRO(num, str) {\\ \n            printf(\"%d\", num);\\ \n            printf(\" is\");\\ \n            printf(\" %s number\", str);\\ \n            printf(\"\\n\");\\ \n           } \n  \nint main(void) \n{ \n    int num; \n  \n    printf(\"Enter a number: \"); \n    scanf(\"%d\", &amp;num); \n  \n    if (num &amp; 1) \n        MACRO(num, \"Odd\"); \n    else\n        MACRO(num, \"Even\"); \n  \n    return 0; \n} <\/code><\/pre>\n\n\n\n<p>The code will give a compilation error while compiling the code.<\/p>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;narendra@\/media\/partition\/GFG]$ make macro\ncc     macro.c   -o macro\nmacro.c: In function \u2018main\u2019:\nmacro.c:19:2: error: \u2018else\u2019 without a previous \u2018if\u2019\nmake: *** &#91;macro] Error 1\n&#91;narendra@\/media\/partition\/GFG]$ <\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-to-illustrate-macro-ends-with-the-semicolon\">Example-to illustrate macro ends  with the semicolon<\/h2>\n\n\n\n<p>In C and C++, statements should end with a semicolon. However, when defining a macro, the semicolon at the end of the macro is required. In the provided example, the mistake is not in including the curly braces in the macro, but rather in including the semicolon at the end of the macro. The correct way to define the macro is with the semicolon:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if (num &amp; 1)\n{\n    -------------------------\n    ---- Macro expansion ----\n    -------------------------\n};    \/* Semicolon at the end of MACRO, and here is ERROR *\/\n\nelse \n{\n   -------------------------\n   ---- Macro expansion ----\n   -------------------------\n\n};<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-to-illustrate-modified-macro\">Example- to illustrate Modified macro<\/h2>\n\n\n\n<p>The placement of a semicolon after the &#8220;if&#8221; statement when the macro is used in the code. To address this issue and ensure that the macro behaves correctly in all situations, it&#8217;s a common practice to enclose the macro in a <code>do-while(0)<\/code> loop. This is often referred to as the &#8220;do-while(0) trick.&#8221; The modified macro will look like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\n#include &lt;stdio.h&gt; \n  \n#define MACRO(num, str) do {\\ \n            printf(\"%d\", num);\\ \n            printf(\" is\");\\ \n            printf(\" %s number\", str);\\ \n            printf(\"\\n\");\\ \n           } while(0) \n  \nint main(void) \n{ \n    int num; \n  \n    printf(\"Enter a number: \"); \n    scanf(\"%d\", &amp;num); \n  \n    if (num &amp; 1) \n        MACRO(num, \"Odd\"); \n    else\n        MACRO(num, \"Even\"); \n  \n    return 0; \n} <\/code><\/pre>\n\n\n\n<p>Now, we can compile and run the code above. The output  we get is given below :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;narendra@\/media\/partition\/GFG]$ make macro\ncc     macro.c   -o macro\n&#91;narendra@\/media\/partition\/GFG]$ .\/macro \nEnter a number: 9\n9 is Odd number\n&#91;narendra@\/media\/partition\/GFG]$ .\/macro \nEnter a number: 10\n10 is Even number\n&#91;narendra@\/media\/partition\/GFG]$ <\/code><\/pre>\n\n\n\n<p>In C or C++ programming, when you create a multi-line macro, you might encounter issues with semicolon placement. To avoid this problem, there are two common techniques:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>The &#8220;do-while(0)&#8221; Trick:<\/strong> You can enclose your multi-line macro within a <code>do-while(0)<\/code> loop. This loop executes only once, and it helps ensure that your macro can be used safely in complex code structures.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\n#include &lt;stdio.h&gt; \n  \n#define MACRO(num, str) ({\\ \n            printf(\"%d\", num);\\ \n            printf(\" is\");\\ \n            printf(\" %s number\", str);\\ \n            printf(\"\\n\");\\ \n           }) \n  \nint main(void) \n{ \n    int num; \n  \n    printf(\"Enter a number: \"); \n    scanf(\"%d\", &amp;num); \n  \n    if (num &amp; 1) \n        MACRO(num, \"Odd\"); \n    else\n        MACRO(num, \"Even\"); \n  \n    return 0; \n} <\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;narendra@\/media\/partition\/GFG]$ make macro\ncc     macro.c   -o macro\n&#91;narendra@\/media\/partition\/GFG]$ .\/macro \nEnter a number: 10\n10 is Even number\n&#91;narendra@\/media\/partition\/GFG]$ .\/macro \nEnter a number: 15\n15 is Odd number\n&#91;narendra@\/media\/partition\/GFG]$ <\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"faq-multiline-macros-in-c\">FAQ- Multiline macros in 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-1699427580945\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1.How to create a multiline macro in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. In C or C++ macros, each line should end with a backslash (\\) for multiline macros. If you use curly braces without proper enclosure, it may lead to errors. To ensure correct behavior, enclose the entire multiline macro within parentheses. This keeps it functioning as a single statement in your code.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1699427587944\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. How do you separate a multi-line macro in C language?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. In C, macros allow you to replace one part of code with another. You can create multiline functionality with macros, but instead of using new lines, you use the backslash (&#8216;\\&#8217;) to separate different parts of the macro.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1699427594910\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. What are the two types of macros in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. In C, you have two types of macros: function-like macros and object-like macros. Function-like macros are created with a code snippet that can take parameters, while object-like macros are defined with a simple value.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Multiline Macros In C A multi-line macro allows you to create a more complex and reusable code block. In C\/C++, you can define a multi-line macro by using the backslash (\\) at the end of each line to continue the macro definition on the next line. Here&#8217;s an example of a multi-line macro that checks &#8230; <a title=\"Multiline Macros In C\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/multiline-macros-in-c\/\" aria-label=\"More on Multiline Macros In C\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":5412,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[556],"class_list":["post-3318","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-multiline-mcros-in-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\/3318","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=3318"}],"version-history":[{"count":12,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/3318\/revisions"}],"predecessor-version":[{"id":7924,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/3318\/revisions\/7924"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/5412"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=3318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=3318"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=3318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}