intがあるのとないのでどう違う

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
メル

intがあるのとないのでどう違う

#1

投稿記事 by メル » 8年前

20人の100メートル走の平均と記録の呼び出しを作っていたのですが下の7行目の for(int i=1; i<=20; i++)にエラーが出てintを消したら実行することができました。成功できたらそれで正解なんだよと突っ込まれてしまいそうですがなんでエラーが出てしまうのか知りたいので教えていただけたらと思います。
#include <stdio.h>

int main()
{
//a=生徒の数、m=100m走、sum=平均、k=記録、t=タイム、b=終了
int a[20], m=100, sum=0, k=0, t=0, b=0,i;
for(int i=1; i<=20; i++)
{

アバター
プラム
記事: 164
登録日時: 10年前
住所: 東海地方

Re: intがあるのとないのでどう違う

#2

投稿記事 by プラム » 8年前

「i」という変数が二回定義されています。

再定義されているということが理由でエラーが出てるんだと思います

アバター
みけCAT
記事: 6734
登録日時: 13年前
住所: 千葉県
連絡を取る:

Re: intがあるのとないのでどう違う

#3

投稿記事 by みけCAT » 8年前

エラーが出るのは、コンパイラが入力の言語に対応していないからでしょう。
ノウルver2.7.7v さんが書きました:「i」という変数が二回定義されています。
はい、そうですね。それは全く悪いことではありません。
ノウルver2.7.7v さんが書きました:再定義されているということが理由でエラーが出てるんだと思います
それは勘違いでしょう。
続きを補い、適切なコンパイラと設定を用いれば、きちんとコンパイルが通ります
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

djann
記事: 27
登録日時: 11年前

Re: intがあるのとないのでどう違う

#4

投稿記事 by djann » 8年前

コード:

#include <stdio.h>

int main()
{
	//a=生徒の数、m=100m走、sum=平均、k=記録、t=タイム、b=終了
	int a[20], m=100, sum=0, k=0, t=0, b=0,i;
	for(int i=1; i<=20; i++)
ですね。6行目?の最後で、既に識別子 i が宣言されており、7行目のint iの部分で多重定義となります。

また、環境が分からないのですが、もしC99以降をコンパイル出来る環境にない場合にC言語としてコンパイルされているのなら、事前に i が宣言されていなくともエラーとなります。
その際の理由としては、C99より前のバージョンの規格では、変数宣言はブロックの先頭でしか行えなかったからです。C99以降のバージョンやC++ではそのような制限はないので、よく見るfor ( int i = 0; i < 20; ++i ){...}のような形で書けます。

djann
記事: 27
登録日時: 11年前

Re: intがあるのとないのでどう違う

#5

投稿記事 by djann » 8年前

おっと、識別子がかぶっても問題ないんでしたっけ。
コード規約がいつの間にか言語規格とごっちゃになってしまったパターンですね反省します。

アバター
みけCAT
記事: 6734
登録日時: 13年前
住所: 千葉県
連絡を取る:

Re: intがあるのとないのでどう違う

#6

投稿記事 by みけCAT » 8年前

N1256より引用
6.2.1 Scopes of identifiers さんが書きました: If the declarator or type specifier that
declares the identifier appears inside a block or within the list of parameter declarations in
a function definition, the identifier has block scope, which terminates at the end of the
associated block.
(中略)
If an identifier designates two different entities in the same name
space, the scopes might overlap. If so, the scope of one entity (the inner scope) will be a
strict subset of the scope of the other entity (the outer scope). Within the inner scope, the
identifier designates the entity declared in the inner scope; the entity declared in the outer
scope is hidden (and not visible) within the inner scope.
6.8.5 Iteration statements さんが書きました: An iteration statement is a block whose scope is a strict subset of the scope of its
enclosing block. The loop body is also a block whose scope is a strict subset of the scope
of the iteration statement.
6.8.5.3 The for statement さんが書きました: The statement
for ( clause-1 ; expression-2 ; expression-3 ) statement
behaves as follows: The expression expression-2 is the controlling expression that is
evaluated before each execution of the loop body. The expression expression-3 is
evaluated as a void expression after each execution of the loop body. If clause-1 is a
declaration, the scope of any identifiers it declares is the remainder of the declaration and
the entire loop, including the other two expressions; it is reached in the order of execution
before the first evaluation of the controlling expression. If clause-1 is an expression, it is
evaluated as a void expression before the first evaluation of the controlling expression.
すなわち、ループはブロックであり、ブロック内で宣言された変数はそのブロック内でのみ有効であるので、

コード:

void hoge(void) {
    for ( clause-1 ; expression-2 ; expression-3 ) statement
}
みたいなプログラムは

コード:

void hoge(void) {
    {
        clause-1;
        loop_start:
        if (!(expression-2)) goto loop_end;
        {
            statement
        }
        loop_end:
        expression-3;
        goto loop_start;
    }
}
のように書き直せると考えられます。
従って、for文の外側で宣言された変数とfor文の内側で宣言された変数の名前が被っても、
単にfor文の内側(で宣言された変数が宣言されたブロック)からfor文の外側の同じ名前の変数が見えなくなるだけで、コンパイルエラーにはならないでしょう。
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

box
記事: 2002
登録日時: 13年前

Re: intがあるのとないのでどう違う

#7

投稿記事 by box » 8年前

とりあえずコード全体を見せてほしいです。
バグのないプログラムはない。
プログラムは思ったとおりには動かない。書いたとおりに動く。

アバター
みけCAT
記事: 6734
登録日時: 13年前
住所: 千葉県
連絡を取る:

Re: intがあるのとないのでどう違う

#8

投稿記事 by みけCAT » 8年前

box さんが書きました:とりあえずコード全体を見せてほしいです。
それより使用しているコンパイラとエラーメッセージを教えてほしいです。
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

メル

Re: intがあるのとないのでどう違う

#9

投稿記事 by メル » 8年前

定義が繰り返されたことによるエラーのようですね。勉強になりました。

ISLe
記事: 2650
登録日時: 13年前
連絡を取る:

Re: intがあるのとないのでどう違う

#10

投稿記事 by ISLe » 8年前

for文 - Wikipediaによると
古いC++では暗黙のブロックはなかったため、(中略)コンパイルエラーとなった。
だそうです。

閉鎖

“C言語何でも質問掲示板” へ戻る