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

59 lines
2.7 KiB
Markdown
Raw Normal View History

2019-03-31 11:00:21 +00:00
---
title: 『C言語ポインタ完全制覇 (標準プログラマーズライブラリ)』
author: kazu634
date: 2008-07-06
url: /2008/07/06/_1035/
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>