c - IF 语句中定义的 undefined variable 出错

标签 c gcc gcc4.8

尝试编译以下代码片段时:

#include <stdio.h>
#include <time.h>

void change_a(int * a) {
    *a = 1;
    return;
}

void main(void) {
    int a = 0;
    change_a(&a);
    if (a) {
        time_t start = time(NULL);
    }
/* do something that never mentions a */

    if (a) {
        time_t end = time(NULL);
       printf("Time spent = %ld\n", (int) end - start);
    }
}

GCC 声明 start 变量在 printf 行中未定义:

$ gcc --version
gcc (Debian 4.8.2-1) 4.8.2
[SNIP]
$ gcc -o test.bin test.c 
test.c: In function ‘main’:
test.c:24:44: error: ‘start’ undeclared (first use in this function)
   printf("Time spent = %ld\n", (int) end - start);

另一方面,将 main 函数更改为:

void main(void) {
    int a = 0;
    time_t start = 0;

    change_a(&a);
    if (a) {
        start = time(NULL);
    }
    ...

问题一

是我做错了什么还是编译器做了一些我不知道的有趣事情?

我认为可能是编译器太聪明了,优化了那段代码,或者它的启发式方法失败了。但每次我“发现编译器错误”时,总是我遗漏了一些非常明显的错误。

因此,在我指责其他聪明人不够聪明之前,我宁愿让聪明人检查一下。特别是当问题在没有优化的情况下也发生时:

$ gcc -O0 -o test.bin test.c 
test.c: In function ‘main’:
test.c:24:44: error: ‘start’ undeclared (first use in this function)
   printf("Time spent = %ld\n", (int) end - start);
                                            ^
test.c:24:44: note: each undeclared identifier is reported only once for each function it appears in

问题2

我还想知道是否有更好的方法来避免编译器错误(如果不是最后一个代码片段中的解决方法)。如果是我的代码错误(因为响应将包含“正确”代码),这是很明显的,但我也想知道如何在有人修复 GCC 中的(所谓的)错误时避免错误。

最佳答案

在您的第一个示例中,start 变量是在 if 语句的范围内声明的。因此,该变量在代码块末尾(右大括号 })超出范围。这绝对不是编译器错误。

您的“解决方法”是正确的解决方案。

See here有关变量作用域如何在 C/C++(以及许多其他使用 C 风格作用域的语言)中工作的更详尽的描述。

关于c - IF 语句中定义的 undefined variable 出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20531407/

相关文章:

c - "%qd"中格式说明符 `printf()` 的用途是什么?

c - Win32 ListView常用控件重画问题

C++ makefile 构建多个 dll 和项​​目结构

c++ - 不匹配 operator== (weak_ptr, const weak_ptr)

c++11 - C++ 启用、禁用类成员函数?

c++ - constexpr 函数不在编译时计算值

gcc - REGSITER_TM_CLONES 中的 Shift 操作的目的是什么?

c++ - 同时追加和读取一个文件

c - 如何使用 WinAPI 识别光驱中的光盘类型?

c - 在整数数组中查找最大/最小出现次数