<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>variadic | Learn C Games Programming Blog</title>
	<atom:link href="https://learncgames.com/tag/variadic/feed/" rel="self" type="application/rss+xml" />
	<link>https://learncgames.com</link>
	<description>A blog about C, programming games and my ebook(s).</description>
	<lastBuildDate>Fri, 08 May 2020 12:05:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://learncgames.com/wp-content/uploads/2020/03/cropped-favicon-32x32.png</url>
	<title>variadic | Learn C Games Programming Blog</title>
	<link>https://learncgames.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">181446779</site>	<item>
		<title>Using printf type variable parameters in your function</title>
		<link>https://learncgames.com/using-printf-type-variable-parameters-in-your-function/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=using-printf-type-variable-parameters-in-your-function</link>
		
		<dc:creator><![CDATA[David]]></dc:creator>
		<pubDate>Tue, 19 May 2020 23:00:29 +0000</pubDate>
				<category><![CDATA[C]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Source code]]></category>
		<category><![CDATA[Techniques]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[variadic]]></category>
		<guid isPermaLink="false">https://learncgames.com/?p=641</guid>

					<description><![CDATA[<p>I needed this in a bit of debug code. I wanted it to work like printf where there&#8217;s a format string containing one or more % format specifications and then write this into a buffer and dump it where ever. C has a library stdarg which lets you do this. It&#8217;s not the most intuitive [&#8230;]</p>
The post <a href="https://learncgames.com/using-printf-type-variable-parameters-in-your-function/">Using printf type variable parameters in your function</a> first appeared on <a href="https://learncgames.com">Learn C Games Programming Blog</a>.]]></description>
										<content:encoded><![CDATA[<div class='__iawmlf-post-loop-links' style='display:none;' data-iawmlf-post-links='[{&quot;id&quot;:722,&quot;href&quot;:&quot;https:\/\/docs.microsoft.com\/en-gb\/sysinternals\/downloads\/debugview&quot;,&quot;archived_href&quot;:&quot;http:\/\/web-wp.archive.org\/web\/20220506060827\/https:\/\/docs.microsoft.com\/en-gb\/sysinternals\/downloads\/debugview&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2026-02-06 17:54:37&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-13 08:40:20&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-17 09:55:02&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-20 12:43:20&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-24 23:21:27&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-04 10:13:43&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-08 13:20:42&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-13 06:59:03&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-17 04:40:37&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-21 12:48:49&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-25 06:20:55&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-30 06:45:16&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-09 10:45:00&quot;,&quot;http_code&quot;:206}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-04-09 10:45:00&quot;,&quot;http_code&quot;:206},&quot;process&quot;:&quot;done&quot;}]'></div>
<p><img fetchpriority="high" decoding="async" class="alignleft size-medium wp-image-643" src="https://learncgames.com/wp-content/uploads/2020/05/c-282x300.png" alt="The C programming languag" width="282" height="300" srcset="https://learncgames.com/wp-content/uploads/2020/05/c-282x300.png 282w, https://learncgames.com/wp-content/uploads/2020/05/c-963x1024.png 963w, https://learncgames.com/wp-content/uploads/2020/05/c-768x817.png 768w, https://learncgames.com/wp-content/uploads/2020/05/c.png 1200w" sizes="(max-width: 282px) 100vw, 282px" />I needed this in a bit of debug code. I wanted it to work like printf where there&#8217;s a format string containing one or more % format specifications and then write this into a buffer and dump it where ever.</p>
<p>C has a library <em>stdarg</em> which lets you do this. It&#8217;s not the most intuitive but it&#8217;s definitely worth understanding.</p>
<p>What I&#8217;m wanting to do is a function that does something like this (assume s1,s2 and s3 are char *).</p>
<pre><code class="language-c" data-line="">sprintf(buffer,&quot;Some string values %s %s %s&quot;,s1,s2,s3);
doSomething(buffer);
</code></pre>
<p>But in my own function and with the ability to have 0,1,2 or how ever many parameters without having to write a separate function for each. Kind of what printf does.</p>
<p>Here&#8217;s the code:</p>
<p>&nbsp;</p>
<pre><code class="language-c" data-line="">#include &lt;stdarg.h&gt;
void op(char* s, ...) {
	char buffer[50];
	va_list argptr;
	va_start(argptr, s);
	vsprintf_s(buffer,sizeof(buffer),s,argptr);
	OutputDebugStringA(buffer);
	va_end(argptr);
}</code></pre>
<p>The &#8230; represent the variable number of parameters. it&#8217;s called the variadic operator. To access the actual parameters needs the various va_ macros and types. For instance va_list is a type that manages the list of parameters. The va_start macro takes the list and the parameter before the list. vsprintf_s is the Microsoft secure version of vsprintf. Both are the variable parameter equivalent of sprintf/sprintf_s.</p>
<p>OutputDebugString is the Windows debug string function. Finally the va_end tidies up everything.</p>
<p>So you use this just like printf, except the output goes to the Debug channel and can be picked up in Visual Studio (if debugging) or by running the SysInternals free <a title="Link to DebugView on Microsoft.com" href="https://docs.microsoft.com/en-gb/sysinternals/downloads/debugview" target="_blank" rel="noopener noreferrer">DebugView</a> utility.</p>
<p><strong>Note, the original version of this used <em>OutputDebugString </em>but I found it was outputting gibberish. I correctly guessed that it was linking to OutputDebugStringW ; the MBCS version and changing it to OutputDebugStringA (the ASCII version) fixed it. Something to watch out for on Windows. </strong></p>The post <a href="https://learncgames.com/using-printf-type-variable-parameters-in-your-function/">Using printf type variable parameters in your function</a> first appeared on <a href="https://learncgames.com">Learn C Games Programming Blog</a>.]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">641</post-id>	</item>
	</channel>
</rss>
