在 C 宏中未声明(在此函数中首次使用)
Undeclared (first use in this function) in C Macro
#define CREDITS_COURSE1 4
#define CREDITS_COURSE2 5
#define CREDITS_COURSE3 4
#define CREDITS_COURSE4 4
#define CREDITS_COURSE5 3
#define CREDITS_COURSE6 3
#define CREDITS_COURSE7 2
#define CREDITS_COURSE8 3
#define COURSE(number) CREDITS_COURSE##number
int main()
{ for(int i=1;i<9;i++)
printf("%d\n",COURSE(i));
}
我期待它打印各自定义的值
但它显示错误
error: 'CREDITS_COURSEi' undeclared (first use in this function); did you mean 'CREDITS_COURSE1'?
10 | #define COURSE(number) CREDITS_COURSE##number
| ^~~~~~~~~~~~~~
cc.c:14:19: note: in expansion of macro 'COURSE'
14 | printf("%d\n",COURSE(i));
| ^~~~~~
cc.c:10:24: note: each undeclared identifier is reported only once for each function it appears in
10 | #define COURSE(number) CREDITS_COURSE##number
| ^~~~~~~~~~~~~~
cc.c:14:19: note: in expansion of macro 'COURSE'
14 | printf("%d\n",COURSE(i));
| ^~~~~~
所以你能帮我得到答案吗
例如
COURSE(1) 应该打印出来 4
COURSE(2) 应该打印出 5
绝对不能运行次调用动态变量名。而且绝对不能用宏来做。
您可以改为将数组声明为 const
以使其不可变:
const int CREDITS_COURSE[8] = {4, 5, 4, 4, 3, 3, 2, 3};
或使用 enumeration
:
enum {
course_1 = 4,
course_2 = 5,
course_3 = 4,
course_4 = 4,
course_5 = 3,
course_6 = 3,
course_7 = 2,
course_8 = 3
} CREDITS_COURSE;
错误在第 10 行
#define 课程(数字) CREDITS_COURSE##数字
您不能使用此声明
宏不能在运行时间内在循环内调用,像这样。您可以声明一个常量数组并初始化所有元素
const int credits_course[8] = {4, 5, 4, 4, 3, 3, 2, 3};
C代码如下:
#include<stdio.h>
const int credits_course[8] = {4, 5, 4, 4, 3, 3, 2, 3};
int main()
{ for(int i=0;i<8;i++)
printf("%d\n",credits_course[i]);
return 0;
}
#define CREDITS_COURSE1 4
#define CREDITS_COURSE2 5
#define CREDITS_COURSE3 4
#define CREDITS_COURSE4 4
#define CREDITS_COURSE5 3
#define CREDITS_COURSE6 3
#define CREDITS_COURSE7 2
#define CREDITS_COURSE8 3
#define COURSE(number) CREDITS_COURSE##number
int main()
{ for(int i=1;i<9;i++)
printf("%d\n",COURSE(i));
}
我期待它打印各自定义的值 但它显示错误
error: 'CREDITS_COURSEi' undeclared (first use in this function); did you mean 'CREDITS_COURSE1'?
10 | #define COURSE(number) CREDITS_COURSE##number
| ^~~~~~~~~~~~~~
cc.c:14:19: note: in expansion of macro 'COURSE'
14 | printf("%d\n",COURSE(i));
| ^~~~~~
cc.c:10:24: note: each undeclared identifier is reported only once for each function it appears in
10 | #define COURSE(number) CREDITS_COURSE##number
| ^~~~~~~~~~~~~~
cc.c:14:19: note: in expansion of macro 'COURSE'
14 | printf("%d\n",COURSE(i));
| ^~~~~~
所以你能帮我得到答案吗
例如
COURSE(1) 应该打印出来 4
COURSE(2) 应该打印出 5
绝对不能运行次调用动态变量名。而且绝对不能用宏来做。
您可以改为将数组声明为 const
以使其不可变:
const int CREDITS_COURSE[8] = {4, 5, 4, 4, 3, 3, 2, 3};
或使用 enumeration
:
enum {
course_1 = 4,
course_2 = 5,
course_3 = 4,
course_4 = 4,
course_5 = 3,
course_6 = 3,
course_7 = 2,
course_8 = 3
} CREDITS_COURSE;
错误在第 10 行
#define 课程(数字) CREDITS_COURSE##数字
您不能使用此声明
宏不能在运行时间内在循环内调用,像这样。您可以声明一个常量数组并初始化所有元素
const int credits_course[8] = {4, 5, 4, 4, 3, 3, 2, 3};
C代码如下:
#include<stdio.h>
const int credits_course[8] = {4, 5, 4, 4, 3, 3, 2, 3};
int main()
{ for(int i=0;i<8;i++)
printf("%d\n",credits_course[i]);
return 0;
}