C语言 除非我在开头声明我的变量,否则为什么会得到“错误未声明的标识符”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4774961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why do I get "error undeclared identifier" unless I declare my variable at the beginning?
提问by Programmer
When I have following:
当我有以下内容时:
#include "stdafx.h"
#include<stdio.h>
int main()
{
int val1,val2;
printf("Enter the first value");
scanf("%d",val1);
scanf("%d",&val2);
int c;
c=val1 + val2;
printf(" the value is : %d", c);
return 0; // 0 means no error
}
I get error undeclared identifier c. Also, syntax error. missing ; before type.
我收到错误未声明的标识符 c。另外,语法错误。丢失的 ; 打字前。
However, if I change above to following error disappears. Please help
但是,如果我将上面更改为以下错误就会消失。请帮忙
#include "stdafx.h"
#include<stdio.h>
int main()
{
int val1,val2,c;
printf("Enter the first value");
scanf("%d",&val1);
scanf("%d",&val2);
c=val1 + val2;
printf(" the value is : %d", c);
return 0; // 0 means no error
}
I am running C in VS 2010.
我在 VS 2010 中运行 C。
回答by Pointy
In C, at least back in the old days, variable declarations have to come at the top of the block. C++ is different in that regard.
在 C 中,至少在过去,变量声明必须位于块的顶部。C++ 在这方面有所不同。
edit— apparently C99 is different from C90 in this respect (C99 being essentially the same as C++ on this issue).
编辑——显然 C99 在这方面与 C90 不同(C99 在这个问题上与 C++ 基本相同)。
回答by Clifford
Objects may only be declared at the top of a statement block in ISO C90. You can therefore do this:
在 ISO C90 中只能在语句块的顶部声明对象。因此,您可以这样做:
#include<stdio.h>
int main()
{
int val1,val2;
printf("Enter the first value");
scanf("%d",val1);
scanf("%d",&val2);
// New statement block
{
int c;
c=val1 + val2;
printf(" the value is : %d", c);
}
return 0; // 0 means no error
}
Though it would perhaps be unusual to do so. Contrary to somewhat popular belief, the start of a function is not the only place you can declare an automatic variable. It is more common, rather than creating a dummy block, to use existing statement blocks introduced as part of an ifor forconstruct for example.
尽管这样做可能不寻常。与一些流行的看法相反,函数的开头并不是您可以声明自动变量的唯一地方。例如,更常见的是使用作为or构造的一部分引入的现有语句块,而不是创建一个虚拟块。 iffor
It is useful to enclose caseblocks in { ... }, even though not normally necessary, so that you can introduce temporary case specific variables:
将case块括在 { ... } 中很有用,即使通常不是必需的,以便您可以引入临时特定于案例的变量:
switch( x )
{
case SOMETHING :
{
int case_local = 0 ;
}
break ;
...
}
回答by Oliver Charlesworth
In C90, local variables must allbe declared at the beginning of a functionblock.
在 C90 中,局部变量必须全部在功能块的开始处声明。
回答by Christoph
Microsoft decided against supporting newer revisions of the C language, so you can't mix code and declarations. With MSVC, you're basically stuck with C90, although some selected features (eg long long, restrict) are supported.
Microsoft 决定不支持 C 语言的更新版本,因此您不能混合使用代码和声明。使用 MSVC,您基本上只能使用 C90,尽管支持某些选定的功能(例如long long,restrict)。
My recommendation would be to either switch to C++ or use a different compiler like the MinGW edition of GCC.
回答by Joe Cullity
One other observation. scanf() wants the ADDRESS of the destination, not it's value.
另一项观察。scanf() 想要目标的地址,而不是它的值。
in the top example you are omitting the &in scanf("%d",val1);. In the bottom example it is included scanf("%d",&val1);
在顶部示例您省略&中 的scanf(“%d”,VAL1); . 在底部示例中,它包含scanf("%d",&val1);
"val1" vs "&val1"
“val1”与“&val1”
Shouldn't change the problem with the variable 'c', but probably causing a syntax error somewhere?
不应该改变变量“c”的问题,但可能会在某处导致语法错误?

