{"id":2561,"date":"2024-05-10T07:23:30","date_gmt":"2024-05-10T07:23:30","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=2561"},"modified":"2024-05-10T07:23:30","modified_gmt":"2024-05-10T07:23:30","slug":"variadic-functions-in-c","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/variadic-functions-in-c\/","title":{"rendered":"Variadic Functions 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=\"#variadic-functions-in-c\">Variadic Functions In C<\/a><\/li><li ><a href=\"#program-1-c-program-to-illustrate-the-working-of-variadic-function\">Program 1: C program to illustrate the working of variadic function <\/a><\/li><li ><a href=\"#program-2-c-program-consisting-of-the-variadic-function-largest-number\">Program 2: C program consisting of the variadic function\u00a0LargestNumber()<\/a><\/li><li ><a href=\"#faq-variadic-functions-in-c\">FAQ- Variadic Functions In C<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"variadic-functions-in-c\">Variadic Functions In C<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Variadic functions in C are like flexible functions because they can accept different numbers of arguments. They always have at least one fixed argument, and then you can pass any number of additional arguments. To indicate this flexibility, variadic functions have an ellipsis (&#8230;) as their last parameter.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Syntax<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int function_name(data_type variable_name, ...);\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Values of the passed arguments can be accessed through the&nbsp;header file&nbsp;named as:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdarg.h&gt;\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">&lt;stdarg.h&gt;&nbsp;includes the following methods:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Methods<\/strong><\/td><td><strong>Description<\/strong><\/td><\/tr><tr><td>va_start(va_list ap, argN)<\/td><td>This method will grant access to variadic function arguments. Whereas,  *va_list* will be the pointer to the last fixed argument in the variadic function* and, it is the last fixed argument in the variadic function.&nbsp;Hence, From the above variadic function (function_name (data_type variable_name, \u2026);) provided, We can conclude that variable_name is the last fixed argument making it the argN.  *va_list ap* will be a pointer to argN (variable_name)<\/td><\/tr><tr><td>va_arg(va_list ap, type)<\/td><td>In this method, it will give access to the next variadic function argument. Also, *va_list ap* is the same as above i.e. a pointer to argN*type*, and it  refers to  the data type &nbsp;the *va_list ap* should expect (double, float, int, etc.)<\/td><\/tr><tr><td>va_copy(va_list dest, va_list src)<\/td><td>This method can create a copy of the variadic function arguments.<\/td><\/tr><tr><td>va_end(va_list ap)<\/td><td>This indicates the passing of the variadic function arguments.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note <\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>va_list<\/strong>&nbsp;holds the information needed by&nbsp;<strong>va_start<\/strong>,&nbsp;<strong>va_arg<\/strong>,&nbsp;<strong>va_end<\/strong>, and&nbsp;<strong>va_copy<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"program-1-c-program-to-illustrate-the-working-of-variadic-function\">Program 1: C program to illustrate the working of variadic function <\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C program for the above approach\n \n#include &lt;stdarg.h&gt;\n#include &lt;stdio.h&gt;\n \n\/\/ Variadic function to add numbers\nint AddNumbers(int n, ...)\n{\n    int Sum = 0;\n \n    \/\/ Declaring pointer to the\n    \/\/ argument list\n    va_list ptr;\n \n    \/\/ Initializing argument to the\n    \/\/ list pointer\n    va_start(ptr, n);\n \n    for (int i = 0; i &lt; n; i++)\n   \/\/ Accessing current variable\n        \/\/ and pointing to next one\n        Sum += va_arg(ptr, int);\n \n    \/\/ Ending argument list traversal\n    va_end(ptr);\n \n    return Sum;\n}\n \n\/\/ Driver Code\nint main()\n{\n    printf(\"\\n\\n Variadic functions: \\n\");\n \n    \/\/ Variable number of arguments\n    printf(\"\\n 1 + 2 = %d \",\n           AddNumbers(2, 1, 2));\n \n    printf(\"\\n 3 + 4 + 5 = %d \",\n           AddNumbers(3, 3, 4, 5));\n \n    printf(\"\\n 6 + 7 + 8 + 9 = %d \",\n           AddNumbers(4, 6, 7, 8, 9));\n \n    printf(\"\\n\");\n \n    return 0;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Variadic functions: \n\n 1 + 2 = 3 \n 3 + 4 + 5 = 12 \n 6 + 7 + 8 + 9 = 30<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"program-2-c-program-consisting-of-the-variadic-function-largest-number\">Program 2: C program consisting of the variadic function&nbsp;LargestNumber()<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\/\/ C program for the above approach\n#include &lt;stdarg.h&gt;\n#include &lt;stdio.h&gt;\n \n\/\/ Variadic function to find the largest number\nint LargestNumber(int n, ...)\n{\n      \/\/ Declaring pointer to the\n    \/\/ argument list\n    va_list ptr;\n \n    \/\/ Initializing argument to the\n    \/\/ list pointer\n    va_start(ptr, n);\n   \n    int max = va_arg(ptr, int);\n \n    for (int i = 0; i &lt; n-1; i++) {\n \n        \/\/ Accessing current variable\n        \/\/ and pointing to next\n        int temp = va_arg(ptr, int);\n        max = temp &gt; max ? temp : max;\n    }\n \n    \/\/ End of argument list traversal\n    va_end(ptr);\n \n    return max;\n}\n \n\/\/ Driver Code\nint main()\n{\n    printf(\"\\n\\n Variadic functions: \\n\");\n \n    \/\/ Variable number of arguments\n    printf(\"\\n %d \",\n           LargestNumber(2, 1, 2));\n \n    printf(\"\\n %d \",\n           LargestNumber(3, 3, 4, 5));\n \n    printf(\"\\n %d \",\n           LargestNumber(4, 6, 7, 8, 9));\n \n    printf(\"\\n\");\n \n    return 0;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Variadic functions: \n\n 2 \n 5 \n 9<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"faq-variadic-functions-in-c\">FAQ- Variadic Functions 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-1696844842537\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. What is an example of a variadic function?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. A variadic function is a function that can accept a variable number of arguments. A well-known example is the <code>printf()<\/code> function from the <code>&lt;stdio.h><\/code> header in C. When you declare a variadic function, you use ellipses (&#8230;) as the last parameter to indicate that it can accept different numbers of arguments.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1696844851179\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. How to access variadic arguments in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. To work with variadic functions in C, you use a set of special macros provided by the <code>&lt;stdarg.h><\/code> header file. These macros include <code>va_list<\/code>, <code>va_start<\/code>, <code>va_arg<\/code>, and <code>va_end<\/code>. They help programmers access and work with the variable arguments that are passed to the function.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1696844859764\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. What is the structure of a Va_list?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. The <code>va_list<\/code> type in C is used to handle variable arguments in a function, but its internal structure is compiler-dependent and not necessarily an array. It&#8217;s best to use the provided macros  <code>va_arg<\/code> for safe and portable access to variable arguments.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Variadic Functions In C Variadic functions in C are like flexible functions because they can accept different numbers of arguments. They always have at least one fixed argument, and then you can pass any number of additional arguments. To indicate this flexibility, variadic functions have an ellipsis (&#8230;) as their last parameter. Syntax Values of &#8230; <a title=\"Variadic Functions In C\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/variadic-functions-in-c\/\" aria-label=\"More on Variadic Functions In C\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":5358,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[431],"class_list":["post-2561","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-variadic-functions-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\/2561","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=2561"}],"version-history":[{"count":10,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2561\/revisions"}],"predecessor-version":[{"id":10656,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2561\/revisions\/10656"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/5358"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=2561"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=2561"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=2561"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}