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++)
{
intがあるのとないのでどう違う
Re: intがあるのとないのでどう違う
「i」という変数が二回定義されています。
再定義されているということが理由でエラーが出てるんだと思います
再定義されているということが理由でエラーが出てるんだと思います
Re: intがあるのとないのでどう違う
エラーが出るのは、コンパイラが入力の言語に対応していないからでしょう。
続きを補い、適切なコンパイラと設定を用いれば、きちんとコンパイルが通ります。
はい、そうですね。それは全く悪いことではありません。ノウルver2.7.7v さんが書きました:「i」という変数が二回定義されています。
それは勘違いでしょう。ノウルver2.7.7v さんが書きました:再定義されているということが理由でエラーが出てるんだと思います
続きを補い、適切なコンパイラと設定を用いれば、きちんとコンパイルが通ります。
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)
Re: 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++)
また、環境が分からないのですが、もしC99以降をコンパイル出来る環境にない場合にC言語としてコンパイルされているのなら、事前に i が宣言されていなくともエラーとなります。
その際の理由としては、C99より前のバージョンの規格では、変数宣言はブロックの先頭でしか行えなかったからです。C99以降のバージョンやC++ではそのような制限はないので、よく見るfor ( int i = 0; i < 20; ++i ){...}のような形で書けます。
Re: intがあるのとないのでどう違う
おっと、識別子がかぶっても問題ないんでしたっけ。
コード規約がいつの間にか言語規格とごっちゃになってしまったパターンですね反省します。
コード規約がいつの間にか言語規格とごっちゃになってしまったパターンですね反省します。
Re: intがあるのとないのでどう違う
N1256より引用
のように書き直せると考えられます。
従って、for文の外側で宣言された変数とfor文の内側で宣言された変数の名前が被っても、
単にfor文の内側(で宣言された変数が宣言されたブロック)からfor文の外側の同じ名前の変数が見えなくなるだけで、コンパイルエラーにはならないでしょう。
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) {
{
clause-1;
loop_start:
if (!(expression-2)) goto loop_end;
{
statement
}
loop_end:
expression-3;
goto loop_start;
}
}
従って、for文の外側で宣言された変数とfor文の内側で宣言された変数の名前が被っても、
単にfor文の内側(で宣言された変数が宣言されたブロック)からfor文の外側の同じ名前の変数が見えなくなるだけで、コンパイルエラーにはならないでしょう。
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)
Re: intがあるのとないのでどう違う
それより使用しているコンパイラとエラーメッセージを教えてほしいです。box さんが書きました:とりあえずコード全体を見せてほしいです。
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)
Re: intがあるのとないのでどう違う
for文 - Wikipediaによると
だそうです。古いC++では暗黙のブロックはなかったため、(中略)コンパイルエラーとなった。