首页 新闻 赞助 找找看

关于标识符的问题 ,为啥就把定义的位置换了就出问题了?

0
悬赏园豆:20 [待解决问题]

我在抄书上的程序的时候出现了这个问题

错误 4 error C2065: “INF”: 未声明的标识符 d:\al_code\suanfa3.c\suanfa3.c\suanfa3.c.cpp 17 1 suanfa3.c

有错误的代码是

#define LOCAL
#define INF 100000
#include "stdafx.h"
#include <stdio.h>
#include "math.h"
//#include <time.h>

int main()
{
#ifdef LOCAL
    freopen("data.in","r",stdin);
    freopen("data.out","w",stdout);
#endif

    int x,n=0,min= INF,max= -INF, s = 0;
    while(scanf("%d",&n)==1)
    {
        s+=x;
        if(x<min)min=x;
        if(x>max)max=x;

    /*    printf("x = %d, min = %d, max = %d \n",x,min,max);*/

        n++;

    }

    printf("%d%d %.3lf\n",min,max,(double)s/n);
        return 0;
}

我想了想,把 #define INF 1000000放到了 main()函数的上面,在#include<>的下面 就无错误的了。

正确代码是

#define LOCAL

#include "stdafx.h"
#include <stdio.h>
#include "math.h"
//#include <time.h>
#define INF 100000
int main()
{
#ifdef LOCAL
    freopen("data.in","r",stdin);
    freopen("data.out","w",stdout);
#endif

    int x,n=0,min= INF,max= -INF, s = 0;
    while(scanf("%d",&n)==1)
    {
        s+=x;
        if(x<min)min=x;
        if(x>max)max=x;

    /*    printf("x = %d, min = %d, max = %d \n",x,min,max);*/

        n++;

    }

    printf("%d%d %.3lf\n",min,max,(double)s/n);
        return 0;
}

请问 ,为啥就把定义的位置换了就出问题了?

混沌奇迹的主页 混沌奇迹 | 初学一级 | 园豆:70
提问于:2013-11-09 16:21
< >
分享
所有回答(1)
0

因为#include相当于把库函数内容复制一遍,而宏定义则是在库函数的基础上看是否被定义,才不会出现错误。我也不太懂,应该是这样吧

冷灵殇月 | 园豆:179 (初学一级) | 2015-05-11 15:34
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册