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;
}
相关推荐
CHANG_THE_WORLD7 分钟前
Python 字符串全面解析
开发语言·python
不会c嘎嘎16 分钟前
深入理解 C++ 异常机制:从原理到工程实践
开发语言·c++
永远都不秃头的程序员(互关)36 分钟前
C语言 基本语法
c语言·开发语言
永远都不秃头的程序员(互关)1 小时前
Java核心技术精要:高效实践指南
java·开发语言·性能优化
是Dream呀1 小时前
Python圣诞特辑:打造一棵会唱歌、会下雪的魔法圣诞树
开发语言·python·pygame
未来之窗软件服务1 小时前
幽冥大陆(四十一)美萍V10酒店门锁SDK C#语言仙盟插件——东方仙盟筑基期
开发语言·c#·仙盟创梦ide·东方仙盟·东方仙盟sdk·酒店智能门锁·东方仙盟 vos 智能浏览器
赖small强2 小时前
【Linux C/C++开发】Linux 平台 Stack Protector 机制深度解析
linux·c语言·c++·stack protector·stack-protector·金丝雀机制
freedom_1024_2 小时前
红黑树底层原理拆解
开发语言·数据结构·b树
liu****3 小时前
3.链表讲解
c语言·开发语言·数据结构·算法·链表
小灰灰搞电子3 小时前
Rust 动态分发(dyn Trait)详解
开发语言·后端·rust