blog/content/post/2008/07/06/2008-07-06-『c言語ポインタ完全制覇-標準プ...

59 lines
2.7 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: 『C言語ポインタ完全制覇 (標準プログラマーズライブラリ)』
author: kazu634
date: 2008-07-06
wordtwit_post_info:
- 'O:8:"stdClass":13:{s:6:"manual";b:0;s:11:"tweet_times";i:1;s:5:"delay";i:0;s:7:"enabled";i:1;s:10:"separation";s:2:"60";s:7:"version";s:3:"3.7";s:14:"tweet_template";b:0;s:6:"status";i:2;s:6:"result";a:0:{}s:13:"tweet_counter";i:2;s:13:"tweet_log_ids";a:1:{i:0;i:4121;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}'
categories:
- C
- Programming
---
<div class="section">
<h4>
可変長引数
</h4>
<p>
 stdarg.hをインクルードする必要があるらしい。
</p>
<pre class="syntax-highlight">
<span class="synComment">/* ======================= */</span>
<span class="synComment">/* === Include library === */</span>
<span class="synComment">/* ======================= */</span>
<span class="synPreProc">#include </span><span class="synConstant">&#60;stdio.h&#62;</span>
<span class="synPreProc">#include </span><span class="synConstant">&#60;stdarg.h&#62;</span>
<span class="synPreProc">#include </span><span class="synConstant">&#60;assert.h&#62;</span>
<span class="synComment">/* ================= */</span>
<span class="synComment">/* === Functions === */</span>
<span class="synComment">/* ================= */</span>
<span class="synType">void</span> tiny_printf(<span class="synType">char</span> *format, ...)
{
<span class="synType">int</span> i;
<span class="synType">va_list</span> ap;
va_start(ap, format);
<span class="synStatement">for</span> (i = <span class="synConstant"></span>; format[i] != <span class="synSpecial">'\0'</span>; i++)
{
<span class="synStatement">switch</span> (format[i]){
<span class="synStatement">case</span> <span class="synConstant">'s'</span>:
printf(<span class="synConstant">&#34;</span><span class="synSpecial">%s</span><span class="synConstant"> &#34;</span>, va_arg(ap, <span class="synType">char</span>*));
<span class="synStatement">break</span>;
<span class="synStatement">case</span> <span class="synConstant">'d'</span>:
printf(<span class="synConstant">&#34;</span><span class="synSpecial">%d</span><span class="synConstant"> &#34;</span>, va_arg(ap, <span class="synType">int</span>));
<span class="synStatement">break</span>;
<span class="synStatement">default</span>:
assert(<span class="synConstant"></span>);
}
}
va_end(ap);
putchar(<span class="synSpecial">'\n'</span>);
}
<span class="synType">int</span> main(<span class="synType">void</span>)
{
tiny_printf(<span class="synConstant">&#34;sdd&#34;</span>, <span class="synConstant">&#34;result...&#34;</span>, <span class="synConstant">3</span>, <span class="synConstant">5</span>);
<span class="synStatement">return</span> <span class="synConstant"></span>;
}
</pre>
</div>