C 将预处理器变量更改为运行时相关变量
我有以下代码:C 将预处理器变量更改为运行时相关变量,c,global-variables,constants,c-preprocessor,C,Global Variables,Constants,C Preprocessor,我有以下代码: #include <stdio.h> #include <stdarg.h> #define A 10 #define B 20 #define C 30 int m = A + B; const int n = A + B + C; void foo1(int x) { m += A + B + x; } void foo2(int x = B, int y = C, ...) { m += B + C + x; if (
#include <stdio.h>
#include <stdarg.h>
#define A 10
#define B 20
#define C 30
int m = A + B;
const int n = A + B + C;
void foo1(int x) {
m += A + B + x;
}
void foo2(int x = B, int y = C, ...) {
m += B + C + x;
if (m > n) foo1(y);
/* Some statements here */
}
/* And MUCH MORE functions and some global variables like these here. */
int main() {
/* Some statements here */
return 0;
}
#包括
#包括
#定义一个10
#定义B20
#定义C30
int m=A+B;
常数n=A+B+C;
void foo1(int x){
m+=A+B+x;
}
void foo2(整数x=B,整数y=C,…){
m+=B+C+x;
如果(m>n)foo1(y);
/*这里有一些陈述*/
}
/*还有更多的函数和一些全局变量*/
int main(){
/*这里有一些陈述*/
返回0;
}
我希望这些
A
,B
,C
作为依赖于运行时的变量,将在main()
函数中处理(无#定义
)。在不更改大多数代码的情况下,将预处理器变量更改为依赖于运行时的变量的最佳方法是什么?(假设整个代码超过1000行。)由于没有人回答我的问题,我最终根据我的实际工作回答了这个问题。尽管我不确定是否有更好的方法将预处理器变量更改为依赖于运行时的变量
解决问题的方法:
#include <stdio.h>
#include <stdarg.h>
/* Preprocessor variable names as global variables */
int A, B, C;
/* Assignments are moved */
int m;
int n; /* no more constant */
void foo1(int x) {
m += A + B + x;
}
void foo2(int x = B, int y = C, ...) {
m += B + C + x;
if (m > n) foo1(y);
/* Some statements here */
}
/* And MUCH MORE functions and some global variables like these here. */
int main() {
/*
A, B, C actually runtime dependant.
But for simplicity, they are hardcoded here.
*/
A = 10;
B = 20;
C = 30;
/* Assignment of global variables */
m = A + B;
n = A + B + C;
/* Some statements here */
return 0;
}
- 首先,取消对代码中必要的“#define”语句的注释。这将突出显示与预处理器变量相关的变量中的错误(例如,通过Visual Studio Intellisense)
- 然后将突出显示的变量赋值移动到main函数(或其他函数)中。您不需要将变量移到函数内部,而是移到所有函数外部(即全局变量)。如果可能,使用查找和替换、正则表达式、宏自动化完成任务
- 然后全局声明预处理器变量名。常量变量将消失,但我认为别无选择
- 最后,检查代码中的错误,必要时进行调试
#include <stdio.h>
#include <stdarg.h>
/* Preprocessor variable names as global variables */
int A, B, C;
/* Assignments are moved */
int m;
int n; /* no more constant */
void foo1(int x) {
m += A + B + x;
}
void foo2(int x = B, int y = C, ...) {
m += B + C + x;
if (m > n) foo1(y);
/* Some statements here */
}
/* And MUCH MORE functions and some global variables like these here. */
int main() {
/*
A, B, C actually runtime dependant.
But for simplicity, they are hardcoded here.
*/
A = 10;
B = 20;
C = 30;
/* Assignment of global variables */
m = A + B;
n = A + B + C;
/* Some statements here */
return 0;
}
预防胜于治疗。应保持计划良好的项目。如果存在依赖于运行时的变量,则不要对其使用预处理器语句
问题代码的最终代码:
#include <stdio.h>
#include <stdarg.h>
/* Preprocessor variable names as global variables */
int A, B, C;
/* Assignments are moved */
int m;
int n; /* no more constant */
void foo1(int x) {
m += A + B + x;
}
void foo2(int x = B, int y = C, ...) {
m += B + C + x;
if (m > n) foo1(y);
/* Some statements here */
}
/* And MUCH MORE functions and some global variables like these here. */
int main() {
/*
A, B, C actually runtime dependant.
But for simplicity, they are hardcoded here.
*/
A = 10;
B = 20;
C = 30;
/* Assignment of global variables */
m = A + B;
n = A + B + C;
/* Some statements here */
return 0;
}
#包括
#包括
/*预处理器变量名作为全局变量*/
INTA、B、C;
/*作业被移动*/
int m;
int n;/*不再是常数*/
void foo1(int x){
m+=A+B+x;
}
void foo2(整数x=B,整数y=C,…){
m+=B+C+x;
如果(m>n)foo1(y);
/*这里有一些陈述*/
}
/*还有更多的函数和一些全局变量*/
int main(){
/*
A、 B,C实际上取决于运行时。
但为了简单起见,这里对它们进行了硬编码。
*/
A=10;
B=20;
C=30;
/*全局变量的赋值*/
m=A+B;
n=A+B+C;
/*这里有一些陈述*/
返回0;
}
>默认参数表示法是无效C,虽然它在C++中是有效的。你在学习哪种语言?最简单的方法就是用全局变量替换它们。这可能不是个好主意。@JonathanLeffler好的,我编辑了这个问题,因为我不确定。顺便说一下,我正在学C。