blog/content/post/2008/05/01/2008-05-01-00000902.md

67 lines
3.1 KiB
Markdown
Raw Normal View History

2019-03-31 11:00:21 +00:00
---
title: 『新版C言語プログラミングレッスン 入門編』第9章・配列
author: kazu634
date: 2008-05-01
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:3963;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}'
categories:
- C
- Programming
---
<div class="section">
<p>
 続きー
</p>
<h4>
配列の宣言の仕方
</h4>
<p>
 こんな感じで宣言する。
</p>
<pre class="syntax-highlight">
<span class="synType">int</span> score[<span class="synConstant">3</span>];
</pre>
<p>
ここで注意すべきは宣言するときは「要素数」を指定すること。添え字の最大値は「要素数-1」だよ
</p>
<p>
<a name="seemore"></a>
</p>
<p>
サンプルはこんな感じだよ:
</p>
<pre class="syntax-highlight">
<span class="synPreProc">#include </span><span class="synConstant">&#60;stdio.h&#62;</span>
<span class="synComment">/* プロトタイプ宣言 */</span>
<span class="synType">int</span> main(<span class="synType">void</span>);
<span class="synComment">/* 関数 main */</span>
<span class="synType">int</span> main(<span class="synType">void</span>)
{
<span class="synType">int</span> ten[<span class="synConstant">3</span>];
<span class="synType">float</span> heikin;
ten[<span class="synConstant"></span>] = <span class="synConstant">65</span>;
ten[<span class="synConstant">1</span>] = <span class="synConstant">90</span>;
ten[<span class="synConstant">2</span>] = <span class="synConstant">75</span>;
heikin = (ten[<span class="synConstant"></span>] + ten[<span class="synConstant">1</span>] + ten[<span class="synConstant">2</span>]) / <span class="synConstant">3.0</span>;
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>, ten[<span class="synConstant"></span>]);
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>, ten[<span class="synConstant">1</span>]);
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>, ten[<span class="synConstant">2</span>]);
printf (<span class="synConstant">&#34;平均点は </span><span class="synSpecial">%1f</span><span class="synConstant">&#34;</span>, heikin);
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>
<p>
二次元とか色々できるけど、そこらへんはわかりそうだから省略。
</p>
</div>