我写了一个非常简单的 C 程序;它是 main.c constants.c 和 constants.h
这是main.c
#include <stdio.h>
#include <stdlib.h>
#include "constants.h"
int main(){
printf("a=%f\n",a);
return 0;
}
这是constants.c
#include "constants.h"
float a = 1.0;
float b = 3.0;
float c = a + b;
这是constants.h
extern float a;
extern float b;
extern float c;
在我尝试用 gcc 编译它之后,我得到以下错误:
error: initializer element is not constant
float c = a + b;
我在 Internet 上阅读了收到该消息的原因,但我找不到任何实际的解决方案;我的目标是通过保持代码结构(3 个文件)来获得 c = a + b
;谁能告诉我应该怎么做才能实现我的目标?
我忘了提一件重要的事情:我不能使用#define
作为可能的解决方案
最佳答案
#define A 1.0
#define B 3.0
float a = A;
float b = B;
float c = A+B;
关于c - 我需要帮助在简单的 C 代码中声明参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28569028/