blog/content/post/2008-04-26-00000890.md

94 lines
4.7 KiB
Markdown

---
title: 『新版C言語プログラミングレッスン 入門編』第六章 for
author: kazu634
date: 2008-04-26
url: /2008/04/26/_956/
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:3943;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}'
categories:
- C
---
<div class="section">
<p>
 久しぶりなんだけど、がんばる。基本、わかるからはやくできそうだ。
</p>
<h4>
ネストが 1 の for
</h4>
<pre class="syntax-highlight">
<span class="synPreProc">#include </span><span class="synConstant">&#60;stdio.h&#62;</span>
<span class="synType">int</span> main(<span class="synType">void</span>);
<span class="synType">int</span> main(<span class="synType">void</span>)
{
<span class="synType">int</span> i;
<span class="synStatement">for</span> (i = <span class="synConstant"></span>;i &#60; <span class="synConstant">10</span>;i++) {
printf (<span class="synConstant">&#34;</span><span class="synSpecial">%d</span><span class="synConstant"> の二乗は </span><span class="synSpecial">%d</span><span class="synConstant"> で、&#34;</span>, i, i * i);
printf (<span class="synConstant">&#34;三乗は </span><span class="synSpecial">%d</span><span class="synConstant"> です。</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>, i * i * i);
}
<span class="synStatement">return</span> (<span class="synConstant"></span>);
}
</pre>
<h4>
ネストが 2 の for
</h4>
<pre class="syntax-highlight">
<span class="synPreProc">#include </span><span class="synConstant">&#60;stdio.h&#62;</span>
<span class="synType">int</span> main(<span class="synType">void</span>);
<span class="synType">int</span> main(<span class="synType">void</span>)
{
<span class="synType">int</span> i, j;
<span class="synStatement">for</span> (i = <span class="synConstant"></span>;i &#60; <span class="synConstant">10</span>;i++) {
printf (<span class="synConstant">&#34;</span><span class="synSpecial">%d</span><span class="synConstant"> &#34;</span>, i);
<span class="synStatement">for</span> (j = <span class="synConstant"></span>;j &#60; i;j++) {
printf (<span class="synConstant">&#34;*&#34;</span>);
}
printf (<span class="synConstant">&#34;</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>);
}
<span class="synStatement">return</span> (<span class="synConstant"></span>);
}
</pre>
<h4>
引数を表示する
</h4>
<pre class="syntax-highlight">
<span class="synPreProc">#include </span><span class="synConstant">&#60;stdio.h&#62;</span>
<span class="synType">int</span> main(<span class="synType">int</span> argc, <span class="synType">char</span> *argv[]);
<span class="synType">int</span> main(<span class="synType">int</span> argc, <span class="synType">char</span> *argv[])
{
<span class="synType">int</span> i;
printf (<span class="synConstant">&#34;argc の値は </span><span class="synSpecial">%d</span><span class="synConstant"> 個あります。</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>, argc);
<span class="synStatement">for</span> (i = <span class="synConstant"></span>;i &#60; argc;i++) {
printf (<span class="synConstant">&#34;argv[</span><span class="synSpecial">%d</span><span class="synConstant">] の値は </span><span class="synSpecial">\&#34;%s\&#34;</span><span class="synConstant">です。</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>, i, argv[i]);
}
<span class="synStatement">return</span> (<span class="synConstant"></span>);
}
</pre>
<h4>
練習問題
</h4>
<pre class="syntax-highlight">
<span class="synPreProc">#include </span><span class="synConstant">&#60;stdio.h&#62;</span>
<span class="synType">int</span> main(<span class="synType">void</span>);
<span class="synType">int</span> main(<span class="synType">void</span>)
{
<span class="synType">int</span> i, j;
<span class="synStatement">for</span> (i = <span class="synConstant"></span>;i &#60; <span class="synConstant">9</span>;i++) {
printf (<span class="synConstant">&#34;</span><span class="synSpecial">%d</span><span class="synConstant"> &#34;</span>, i);
<span class="synStatement">for</span> (j = <span class="synConstant"></span>;j &#60; i * i;j++) {
printf (<span class="synConstant">&#34;*&#34;</span>);
}
printf (<span class="synConstant">&#34;</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>);
}
<span class="synStatement">return</span>(<span class="synConstant"></span>);
}
</pre>
</div>