【发布时间】:2017-10-30 16:57:26
【问题描述】:
我想要 as,如果 i 为 1,则为 int 类型,否则为 char 类型,但是当我编译此代码时,会遇到以下错误:
> 1.c: In function ‘main’:
> 1.c:18:16: error: ‘a’ undeclared (first use in this function)
> if(a)
> ^
> 1.c:18:16: note: each undeclared identifier is reported only once for each function it appears in
#include <stdio.h>
#define macro
void main()
{
int i=1;
#ifdef macro
if (i==1)
{
int a;
}
else
#endif
{
char a;
}
if(a)
{
printf("INDOE ");
}
}
【问题讨论】:
-
变量有一个声明它们的块的范围。
-
您可以将
a声明为int,然后在必要时有条件地将其转换为char? -
你想用 a 做什么?
-
如果这是 C++,您将使用模板(假设
i的值在编译时已知)。 -
rohit 你熟悉
union吗?
标签: c if-statement variable-declaration