<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ahmed Mustafa Haroun on Medium]]></title>
        <description><![CDATA[Stories by Ahmed Mustafa Haroun on Medium]]></description>
        <link>https://medium.com/@harouncdev?source=rss-8b31f668bcce------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*iVvajFKOuEL_rKVdwZRn8g.png</url>
            <title>Stories by Ahmed Mustafa Haroun on Medium</title>
            <link>https://medium.com/@harouncdev?source=rss-8b31f668bcce------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 09:49:33 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@harouncdev/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Functions with variable amounts of arguments in C.]]></title>
            <link>https://medium.com/@harouncdev/functions-with-variable-amounts-of-arguments-in-c-76400c24c317?source=rss-8b31f668bcce------2</link>
            <guid isPermaLink="false">https://medium.com/p/76400c24c317</guid>
            <category><![CDATA[dennis-ritchie]]></category>
            <category><![CDATA[functions-in-c]]></category>
            <category><![CDATA[c]]></category>
            <category><![CDATA[brian-kernighan]]></category>
            <category><![CDATA[variadic-functions]]></category>
            <dc:creator><![CDATA[Ahmed Mustafa Haroun]]></dc:creator>
            <pubDate>Tue, 23 May 2023 21:05:20 GMT</pubDate>
            <atom:updated>2023-05-23T21:20:32.298Z</atom:updated>
            <content:encoded><![CDATA[<p>Variable Argument Functions..</p><p>You’ve seen functions and created functions with a set number of arguments, but if this was the limits to the functions we could create it wouldn’t allow for functions like printf(), which take a variable number of arguments.</p><p>Lets look at how a Function with a variable amount of arguments is defined.</p><p><strong>This is my solution to Excercise 7–3 of K&amp;R where I revised a wrapper around printf to add a format specifier for %p.</strong></p><pre>#include &lt;stdio.h&gt;<br>#include &lt;stdarg.h&gt;<br><br>void minprintf(char *format, ...) {<br>    va_list ap; // pointer to unnamed arguments<br>    char *p, *sval;<br>    int ival;<br>    double dval;<br>    void* vpval;<br><br>    va_start(ap, format);<br>    for (p = format; *p; p++) {<br>        if (*p != &#39;%&#39;) {<br>            putchar(*p);<br>            continue;<br>        }<br>        switch (*++p) {<br>            case &#39;d&#39;:<br>                ival = va_arg(ap, int);<br>                printf(&quot;%d&quot;, ival);<br>                break;<br>            case &#39;f&#39;:<br>                dval = va_arg(ap, double);<br>                printf(&quot;%f&quot;, dval);<br>                break;<br>            case &#39;s&#39;:<br>                for (sval = va_arg(ap, char*); *sval; sval++)<br>                    putchar(*sval);<br>                break;<br>            case &#39;p&#39;:<br>                vpval = va_arg(ap, void*);<br>                printf(&quot;%p&quot;, vpval);<br>                break;<br>            default:<br>                putchar(*p);<br>                break;<br>        }<br>    }<br>    va_end(ap);<br>}<br><br></pre><p>Starting with the declaration of the function</p><p>void minprintf(char *format, …)</p><p><strong>read as minprintf is a function that has parameters of a pointer to a character, and a variable length of arguments and returns void</strong></p><p>indicates that my function takes a variable length of arguments. The last parameter of your function should be an ellipses(…) in order to allow for variable amounts of arguments.</p><p>It is required to have at least one parameter in your function before the ellipses in C, usually it is the number and or type of arguments being passed to allow your function to handle the variable arguments appropriately.</p><p><strong>While printf(“The number is %d”, 5); does not explicitly require you to enter the amount of arguments, the number and type of arguments can be derived from the format specifiers in the first required argument of printf().</strong></p><p>va_list ap; // pointer to unnamed arguments</p><p>The variable length of arguments being passed into our function do not have a name.</p><p>Using the &lt;stdarg.h&gt; header we can declare a variable with a type of va_list.</p><p>While it’s implementation depends on your compiler and platform, it is helpful to think of it as a pointer to our unnamed arguments.</p><p>Just like if we were to declare a regular pointer</p><p>char* somestring;</p><p>Both ap and somestring do not point to anything yet, while we might initialize our pointer by assigning it to a string literal like “Hello World”, to initialize our pointer to unnamed arguments we need to use the function like macro provided to us by &lt;stdarg.h&gt;</p><p>va_start(ap, format);</p><p>This macro initializes our “pointer” and uses the last named argument of our function.</p><p>The next function like macro we use is</p><p>ival = va_arg(ap, int);</p><p>va_arg returns the argument pointed at by ap as the type we specify and moves the pointer to the next argument. (in this case it moves ap to point the sizeof(int) bytes forward b/c it also uses the type we specify in order to know how big of a step it should take)</p><p>Then the final function like macro we use is</p><p>va_end(ap);</p><p>Which does any clean up necessary and while its not required when compiling with gcc, or clang it is a good idea to include anyway to maintain portability.</p><p>While reaching the end of K&amp;R’s C programming book, I had trouble understanding variable argument functions and is a summary of all the resources I consumed to get a better understanding of them. Some things may be false, I encourage you to double check my article, I am only a student after all.</p><p>Some useful resources for understanding variable function arguments in C also referred to as variadic functions</p><p><a href="https://en.cppreference.com/w/c/variadic">https://en.cppreference.com/w/c/variadic</a></p><p><a href="https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1548.pdf">https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1548.pdf</a> (page 288)</p><p><a href="https://www.youtube.com/watch?v=S-ak715zIIE&amp;ab_channel=JacobSorber">https://www.youtube.com/watch?v=S-ak715zIIE&amp;ab_channel=JacobSorber</a></p><p><a href="https://github.com/auspbro/ebook-c/blob/master/The.C.Programming.Language.2Nd.Ed%20Prentice.Hall.Brian.W.Kernighan.and.Dennis.M.Ritchie..pdf">https://github.com/auspbro/ebook-c/blob/master/The.C.Programming.Language.2Nd.Ed%20Prentice.Hall.Brian.W.Kernighan.and.Dennis.M.Ritchie..pdf</a> (page 155)</p><p>While looking at the first resource you might stumble onto this function declaration which had me stumped for a little bit</p><pre>int vprintf( const char *format, va_list vlist );</pre><p>I like to think of it as just a regular printf() function but one that accepts that new type we learned va_list which is a “pointer” to a list of arguments.</p><p>Possible use could be inside another function that accepts variable arguments.</p><pre>#include &lt;stdio.h&gt;<br>#include &lt;stdarg.h&gt;<br><br>int myprintf(const char* formatstring, ...){<br>  va_list ap;<br>  va_start(ap, formatstring)<br>  vprintf(formatstring, ap);<br>  va_end(ap);<br>}</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=76400c24c317" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>