宏定义不完整引起的奇怪报错

一 写在开头

1.1 本文内容

记录一次奇怪的报错和解决过程。

 

二 事情经过

今天在写代码的过程遇到了一个很有趣的事。下面的代码快速扫一遍很难发现其存在的问题,但我在编译它时却遇到了意外的报错。代码和报错情况如下。

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 #define FALSE
 5 #define TRUE
 6 #define MAX_RANGE_N     10005
 7 
 8 static char indexs[MAX_RANGE_N];
 9 
10 void ScreenPrimes(char *indexs)
11 {
12     int i, j;
13 
14     memset(indexs, TRUE, sizeof(indexs[0]) * MAX_RANGE_N);
15     indexs[0] = indexs[1] = FALSE;
16 
17     for (i = 2; i < MAX_RANGE_N; i++)
18         if (indexs[i] == TRUE)
19             for (j = i + i; j < MAX_RANGE_N; j += i)
20                 indexs[j] = FALSE;
21 }
22 
23 int main(int argc, char *argv[])
24 {
25     freopen("in.txt", "r", stdin);
26     int i, n, counter = 0;
27 
28     ScreenPrimes(indexs);
29     scanf("%d", &n);
30     for (i = 0; i <= n; i++)
31     {
32         if (indexs[i] == TRUE)
33         {
34             if (((i+2) <= n) && (indexs[i+2] == TRUE))
35             {
36                 printf("%d %d
", i, i+2);
37                 counter++;
38             }
39         }
40         else
41         {
42             continue;
43         }
44     }
45     if (counter == 0)
46         printf("empty
");
47 
48     fclose(stdin);
49     return 0;
50 }

看到没,当时查错的时候硬是完整仔细得看了一遍代码,才发现,报错的原因竟然是宏定义不完整!代码在定义TRUE和FALSE两个宏时后面竟然没写数值!将这两个宏定义改写成如下形式,问题解决!

1 #define FALSE (-1)
2 #define TRUE  0
原文地址:https://www.cnblogs.com/laizhenghong2012/p/8728671.html