{"id":3327,"date":"2024-03-05T12:55:39","date_gmt":"2024-03-05T12:55:39","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=3327"},"modified":"2024-03-05T12:55:39","modified_gmt":"2024-03-05T12:55:39","slug":"variable-length-arguments-for-macros","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/variable-length-arguments-for-macros\/","title":{"rendered":"Variable Length Arguments For Macros"},"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=\"#variable-length-arguments-for-macros\">Variable Length Arguments For Macros<\/a><\/li><li ><a href=\"#example-to-illustrate-variable-length-arguments-in-macros\">Example- to illustrate variable-length arguments in macros <\/a><\/li><li ><a href=\"#faq-variable-length-arguments-for-macros\">FAQ- Variable Length Arguments For Macros<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"variable-length-arguments-for-macros\">Variable Length Arguments For Macros<\/h2>\n\n\n\n<p>In C and C++, macros offer a powerful tool for code simplification and automation. While most commonly associated with simple text substitution, macros can also be equipped to handle variable-length arguments. This capability allows developers to create more flexible and dynamic code constructs, much like functions that accept varying numbers of parameters. <\/p>\n\n\n\n<p>By using features like the &#8220;\u2026&#8221; and &#8220;<strong>VA_ARGS<\/strong>&#8221; preprocessor identifiers, along with the concatenation operator &#8220;##,&#8221; programmers can build macros that adapt to different argument lists, making their code more versatile and efficient. In this context, we explore the concept of variable-length arguments for macros and demonstrate their practical applications in enhancing the functionality of code macros.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-to-illustrate-variable-length-arguments-in-macros\">Example- to illustrate variable-length arguments in macros <\/h2>\n\n\n\n<p>In C, you can use macros to handle variable-length arguments, just like you do with functions. To do this, you use &#8220;&#8230;&#8221; and &#8220;<strong>VA_ARGS<\/strong>&#8221; in your macro definition. The &#8220;##&#8221; operator helps combine these variable arguments.<\/p>\n\n\n\n<p>In C, you can create macros that work like the &#8220;printf()&#8221; function for logging messages. For example, we have a macro called <code>LOG_MESSAGE<\/code>. It takes three arguments: the message priority (&#8220;INFO&#8221; or &#8220;ERROR&#8221;), the output stream (&#8220;stdout&#8221; or &#8220;stderr&#8221;), and a variable number of message contents.<\/p>\n\n\n\n<p>When &#8220;INFO&#8221; is used, it prints informational messages, including the source file and line number. When &#8220;ERROR&#8221; is used, it prints error messages in a similar format. This allows you to easily log different types of messages with context information using a simple macro.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n \n#define INFO     1\n#define ERR    2\n#define STD_OUT    stdout\n#define STD_ERR    stderr\n \n#define LOG_MESSAGE(prio, stream, msg, ...) do {\\\n                        char *str;\\\n                        if (prio == INFO)\\\n                            str = \"INFO\";\\\n                        else if (prio == ERR)\\\n                            str = \"ERR\";\\\n                        fprintf(stream, \"&#91;%s] : %s : %d : \"msg\" \\n\", \\\n                                str, __FILE__, __LINE__, ##__VA_ARGS__);\\\n                    } while (0)\n \nint main(void)\n{\n    char *s = \"Hello\";\n \n        \/* display normal message *\/\n    LOG_MESSAGE(ERR, STD_ERR, \"Failed to open file\");\n \n    \/* provide string as argument *\/\n    LOG_MESSAGE(INFO, STD_OUT, \"%sSkill Vertex\", s);\n \/* provide integer as arguments *\/\n    LOG_MESSAGE(INFO, STD_OUT, \"%d + %d = %d\", 10, 20, (10 + 20));\n \n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;narendra@\/media\/partition\/GFG]$ .\/variable_length \n  &#91;ERR] : variable_length.c : 26 : Failed to open file \n  &#91;INFO] : variable_length.c : 27 : Hello Geeks for Geeks \n  &#91;INFO] : variable_length.c : 28 : 10 + 20 = 30 \n  &#91;narendra@\/media\/partition\/GFG]$<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"faq-variable-length-arguments-for-macros\">FAQ- Variable Length Arguments For Macros<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1699437709705\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. What are variable length arguments with example?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. A variable-length argument is a feature that enables a function to accept any number of arguments. In certain scenarios, functions need to handle a variable quantity of arguments based on specific requirements. For instance, this can be useful for tasks like finding the sum of a set of numbers, determining the minimum value among a collection of numbers, and various other tasks where the number of input arguments can vary. This feature provides flexibility in function design and allows it to adapt to different needs.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1699437721129\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2.What are the types of arguments used with macros?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. <br \/><strong>Positional Arguments:<\/strong> These are arguments given in a specific order, and their values match the parameters in the same order.<br \/><strong>Keyword Arguments:<\/strong> Keyword arguments are associated with parameter names, allowing you to specify arguments by name rather than position, offering clarity and flexibility.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1699437735175\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. How do you declare a variable length argument?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. To accept variable-length parameters in a method, you use an asterisk (*) before the parameter name. These parameters are treated as a tuple within the method, and the tuple takes on the same name as the parameter, minus the asterisk.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Variable Length Arguments For Macros In C and C++, macros offer a powerful tool for code simplification and automation. While most commonly associated with simple text substitution, macros can also be equipped to handle variable-length arguments. This capability allows developers to create more flexible and dynamic code constructs, much like functions that accept varying numbers &#8230; <a title=\"Variable Length Arguments For Macros\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/variable-length-arguments-for-macros\/\" aria-label=\"More on Variable Length Arguments For Macros\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":3330,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[557],"class_list":["post-3327","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-variable-length-arguments-for-macros","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\/3327","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=3327"}],"version-history":[{"count":8,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/3327\/revisions"}],"predecessor-version":[{"id":7925,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/3327\/revisions\/7925"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/3330"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=3327"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=3327"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=3327"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}