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;
}
相关推荐
m0_380743874 分钟前
PHP 8.3 接口返回数据为空怎么排查
开发语言·php
谢亮_vipxieliang14 分钟前
Java 21 新特性实战:Record、Sealed、模式匹配
java·开发语言
霸道流氓气质21 分钟前
LLM 应用限流与熔断机制完全指南:从多层防护架构到Java生产级弹性实战
java·开发语言·架构
大侠归来25 分钟前
C语言内存管理:从栈到堆的完整指南
c语言·开发语言·python
cu14327 分钟前
细谈GM7123C的具体功能与其应用
c语言·c++·人工智能·嵌入式硬件
m0_3807438742 分钟前
PHP7.0字符串在Docker怎么用
开发语言·php
Logic1011 小时前
C语言/数据结构位运算题解:异或XOR找出弹子游戏机中数量为奇数的数字——只出现奇数次
c语言·数据结构·数组·位运算·时间复杂度·算法题·异或性质
朝朝辞暮i1 小时前
C++ 第 10 课:函数 Function
开发语言·c++
朝朝辞暮i1 小时前
C++ 第7课 while 循环
开发语言·c++·算法
小朱爱编程1232 小时前
我用 Jev 做了三个实用工具:整理标签页、分诊飞书反馈、找回 GitHub 收藏
java·开发语言·人工智能·后端·python·架构·ai编程