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;
}
相关推荐
QuantumStack41 分钟前
【C++ 真题】P1104 生日
开发语言·c++·算法
whoarethenext1 小时前
使用 C++/OpenCV 和 MFCC 构建双重认证智能门禁系统
开发语言·c++·opencv·mfcc
代码的奴隶(艾伦·耶格尔)2 小时前
后端快捷代码
java·开发语言
Jay_5152 小时前
C++多态与虚函数详解:从入门到精通
开发语言·c++
路来了2 小时前
Python小工具之PDF合并
开发语言·windows·python
xiaolang_8616_wjl3 小时前
c++文字游戏_闯关打怪
开发语言·数据结构·c++·算法·c++20
WJ.Polar3 小时前
Python数据容器-list和tuple
开发语言·python
small_wh1te_coder3 小时前
硬件嵌入式学习路线大总结(一):C语言与linux。内功心法——从入门到精通,彻底打通你的任督二脉!
linux·c语言·汇编·嵌入式硬件·算法·c
FrostedLotus·霜莲3 小时前
C++主流编辑器特点比较
开发语言·c++·编辑器
超级码.里奥.农3 小时前
零基础 “入坑” Java--- 七、数组(二)
java·开发语言