我正在根据 Beej's Guide to Network Programming 模拟客户端/服务器交互。在我的 server.c
文件的开头,我定义了一个端口号:
#define PORT "21124"
但是,我将从此文件创建多个服务器,并且我想根据一些 if-else 逻辑更改此常量。 C 中是否有功能可以轻松完成此任务,例如:
if (serverNumber == 1) {
#define PORT "21124"
}
else if (serverNumber == 2) {
#define PORT "12412"
}
else {
#define PORT "12334"
}
这可能是重复的,但我在搜索中没有看到它。我是一个 C
菜鸟。
最佳答案
“我想改变这个常量”。这听起来不对,是吗?
因此,只需使用像 int port
这样的变量,并使用简单的 if-else 语句 block 或使用开关为其赋值。
关于c - #define 在 C 中使用 if-else 逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29453554/