94 lines
4.6 KiB
Markdown
94 lines
4.6 KiB
Markdown
---
|
|
title: 『新版C言語プログラミングレッスン 入門編』第六章 for
|
|
author: kazu634
|
|
date: 2008-04-26
|
|
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"><stdio.h></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 < <span class="synConstant">10</span>;i++) {
|
|
printf (<span class="synConstant">"</span><span class="synSpecial">%d</span><span class="synConstant"> の二乗は </span><span class="synSpecial">%d</span><span class="synConstant"> で、"</span>, i, i * i);
|
|
printf (<span class="synConstant">"三乗は </span><span class="synSpecial">%d</span><span class="synConstant"> です。</span><span class="synSpecial">\n</span><span class="synConstant">"</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"><stdio.h></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 < <span class="synConstant">10</span>;i++) {
|
|
printf (<span class="synConstant">"</span><span class="synSpecial">%d</span><span class="synConstant"> "</span>, i);
|
|
<span class="synStatement">for</span> (j = <span class="synConstant"></span>;j < i;j++) {
|
|
printf (<span class="synConstant">"*"</span>);
|
|
}
|
|
printf (<span class="synConstant">"</span><span class="synSpecial">\n</span><span class="synConstant">"</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"><stdio.h></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">"argc の値は </span><span class="synSpecial">%d</span><span class="synConstant"> 個あります。</span><span class="synSpecial">\n</span><span class="synConstant">"</span>, argc);
|
|
<span class="synStatement">for</span> (i = <span class="synConstant"></span>;i < argc;i++) {
|
|
printf (<span class="synConstant">"argv[</span><span class="synSpecial">%d</span><span class="synConstant">] の値は </span><span class="synSpecial">\"%s\"</span><span class="synConstant">です。</span><span class="synSpecial">\n</span><span class="synConstant">"</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"><stdio.h></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 < <span class="synConstant">9</span>;i++) {
|
|
printf (<span class="synConstant">"</span><span class="synSpecial">%d</span><span class="synConstant"> "</span>, i);
|
|
<span class="synStatement">for</span> (j = <span class="synConstant"></span>;j < i * i;j++) {
|
|
printf (<span class="synConstant">"*"</span>);
|
|
}
|
|
printf (<span class="synConstant">"</span><span class="synSpecial">\n</span><span class="synConstant">"</span>);
|
|
}
|
|
<span class="synStatement">return</span>(<span class="synConstant"></span>);
|
|
}
|
|
</pre>
|
|
</div>
|