C语言中比较优雅的错误定义

废话不多说,直接上代码

c 复制代码
#include <stdio.h>
#include <stdlib.h>

#define foreach_http_error				\
  _ (401, "401 error")					\
  _ (501, "501 error")

typedef enum
{
#define _(sym,string) HTTP_ERROR_##sym,
	foreach_http_error
#undef _
	HTTP_N_ERROR,
} http_reply_error_t;

static char *http_error_strings[] = {
#define _(sym,string) string,
	foreach_http_error
#undef _
};

const char *get_error(const int code) {
	return http_error_strings[code];
}

int main() {
	const char *err = get_error(HTTP_ERROR_401);
	printf("err string: %s\n", err);
	getchar();
	return 0;
}
相关推荐
SomeB1oody14 小时前
【RustyML入门】5.2. 分类指标
开发语言·后端·机器学习·rust·教程
ZJU_统一阿萨姆14 小时前
【推理优化进阶】图编译与运行时:动态形状、CUDA Graph 与内存规划
开发语言·人工智能·语言模型·架构·开源
@呱呱爱学习14 小时前
C++大成之路_ STL容器_ Vector
开发语言·c++
额额额对了14 小时前
数据结构:二叉树
c语言·数据结构·算法
码匠许师傅15 小时前
【C++ 面试真题】19. 聊聊 C++ 的迭代器
开发语言·c++·面试
AI情绪识别开源15 小时前
检信ALLEMOTION VibrationAI 2.4.0 12维度情绪识别开源源代码
开发语言·python
Dovis(誓平步青云)15 小时前
DevEco Studio 6.1.1 Windows 安装实录:从下载校验到首次启动
android·开发语言·数据库·人工智能·windows·harmonyos
qeen8715 小时前
【数据结构】红黑树的算法原理解析与实现
开发语言·数据结构·c++·算法·红黑树
Logintern0915 小时前
什么时候应该用多进程什么时候用多线程呢?
开发语言·python
long31615 小时前
枚举(Enums)
java·开发语言·数据库