正确的方式在学习C语言变量时,一个简单的问题困惑我很久,先不多说,看代码和编译错误:
#include <stdio.h>
#define PRICE 30
void main(){
int num , total;
num = 10;
total = PRICE*num;
printf("%d\n",total);
}
输出的结果:300
写法1:
#include <stdio.h>
#define PRICE 30; //注意这边的分号
void main(){
int num , total;
num = 10;
total = PRICE*num;
printf("%d\n",total);
}结果:
写法2:
#include <stdio.h>
#define PRICE 30; //注意这边的分号
void main(){
int num , total;
num = 10;
total = num*PRICE;
printf("%d\n",total);
}输出结果:300
对比写法1和写法2:出现编译错误的原因是:error: invalid type argument of unary ‘*’ (have ‘int’),暂时先把疑问记录在这,待解决。
本文通过两个示例探讨了在C语言中使用宏定义时的一个常见误区,特别是关于宏定义末尾分号的影响,并对比了不同写法下程序的运行结果及编译错误。
1万+





