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;
}
相关推荐
似是燕归来14 小时前
嵌入式工程师进阶指南:适配器编程设计模式一
c语言·stm32·适配器模式·嵌入式开发
西门吹-禅15 小时前
java springboot N+1问题
java·开发语言·spring boot
skywalk816315 小时前
设计并实现段言的 C FFI 绑定机制 @Trae
c语言·开发语言·python·编程
IT笔记17 小时前
【Rust】Rust Match 模式匹配详解
java·开发语言·rust
2zcode17 小时前
免费开源项目文档:基于MATLAB卷积神经网络的口罩佩戴检测系统
开发语言·matlab·cnn
逝水无殇17 小时前
C# 运算符重载详解
开发语言·后端·c#
TPBoreas18 小时前
配置信息防泄露方案:.env 环境隔离详解(dotenv-java)
java·开发语言
额鹅恶饿呃18 小时前
C语言中的数据结构和变量
c语言·数据结构·算法
敲代码的嘎仔18 小时前
实习日志day6--实习日志day6--title命名规范化&businessType纠正&补充缺失的@Log注解&报警与通信模块补充&产出阶段总结文档
java·开发语言·人工智能·git·python·实习·大二
江华森18 小时前
Python 实现高德地图找房(一):环境搭建与数据爬虫
开发语言·爬虫·python