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;
}
相关推荐
ZEERO~14 小时前
@dataclass的作用
开发语言·windows·python
!停14 小时前
C语言单链表
c语言·数据结构·算法
南行*14 小时前
C语言Linux环境编程
linux·c语言·开发语言·网络安全
Morwit14 小时前
Qt qml创建c++类的单例对象
开发语言·c++·qt
古城小栈14 小时前
Rust 已经自举,却仍需GNU与MSVC工具链的缘由
开发语言·rust
jarreyer14 小时前
数据项目分析标准化流程
开发语言·python·机器学习
你怎么知道我是队长14 小时前
C语言---printf函数使用详细说明
c语言·开发语言
liulilittle14 小时前
俄罗斯访问欧洲国际线路优化
开发语言·网络·信息与通信·ip·通信·俄罗斯·莫斯科
陈小桔14 小时前
logging模块-python
开发语言·python
消失的旧时光-194314 小时前
函数指针 + 结构体 = C 语言的“对象模型”?——从 C 到 C++ / Java 的本质统一
linux·c语言·开发语言·c++·c