Iu *_*Tub 0 c compiler-errors const constants c-preprocessor
#include <stdio.h>
#include <stdlib.h>
#define MAX 15 //line that give problems
int linearSearch(int v[], int MAX, int valore);
int main()
{
int ris, valore, v[]={1,1,1,1,1,1,1,1,1,12,1,1,1,1,1};
scanf("%d", &valore);
ris = linearSearch(v, MAX, valore);
printf("%d", ris);
return 0;
}
int linearSearch(int v[], int MAX, int valore)
{
int i;
for (i=0;i<MAX;i++)
{
if(valore==v[i])
return i;
}
return -1;
}
Run Code Online (Sandbox Code Playgroud)
为什么这段代码在编译时报告错误?如果我用预处理器指令替换它,为什么它运行正常
const int MAX = 15;
Run Code Online (Sandbox Code Playgroud)
宏(大多数)是简单的文本替换,因此:
#define MAX 15 //line that give problems
int linearSearch(int v[], int MAX, int valore);
Run Code Online (Sandbox Code Playgroud)
将被预处理为:
int linearSearch(int v[], int 15, int valore);
^
Run Code Online (Sandbox Code Playgroud)
当然,您不能拥有标识符的数值.
| 归档时间: |
|
| 查看次数: |
115 次 |
| 最近记录: |