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;
}
相关推荐
Do_It_Today6 分钟前
php xml 入门学习资料
开发语言·php
熊猫钓鱼>_>15 分钟前
Kotlin Multiplatform for OpenHarmony 实战:为 Landscapist 实现图片加载适配
开发语言·kotlin·华为云·ai编程·harmonyos·鸿蒙·openharmony
Nebula_g1 小时前
JavaSE加强:Commons-io框架
java·开发语言·算法·javase
Do_It_Today1 小时前
php url路由入门实例
开发语言·php
小羊没烦恼!1 小时前
关于大型asp.net应用系统的架构-架构的选择
java·服务器·开发语言·前端·c#
夏幻灵1 小时前
前端八股:JavaScript 深拷贝详解:实现方式、递归原理与循环引用
开发语言·前端·javascript
傲世仙尊1 小时前
C++起步-命名空间解决命名冲突与cin-cout缺省参数
开发语言·c++
hljqwb1 小时前
java服务异常日志只打印异常类型,没有堆栈定位分析(十)
java·开发语言
Zw-awa2 小时前
陀螺小车传奇之00011000
c语言·stm32
ym hyd 1112 小时前
生鲜超市库存管理信息系统源码 Java+SpringBoot+Vue3 前后分离
java·开发语言·vue.js·spring boot·毕设