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;
}
相关推荐
techdashen42 分钟前
Go设计取舍之四: map不变时能否并发修改不同value
开发语言·后端·golang
小小小米粒1 小时前
阿姆达尔定律(Amdahl‘s Law)
java·开发语言
知彼解己1 小时前
Java 版本演进
java·开发语言·spring boot
风样滴男人哟1 小时前
PHP特性之反射类ReflectionClass机制
android·开发语言·php
看昭奚恤哭1 小时前
Flutter 布局核心思想
开发语言·javascript·flutter
IsSh9nj6q2 小时前
Python全栈应用搭建神器magic-dash .新版本介绍
开发语言·python·dash
独隅2 小时前
CLion 在 Linux 上的完整安装与配置使用指南
linux·运维·服务器·c语言·c++·ide
Cachel wood2 小时前
hands-on-modern-rl:动手学强化学习策略梯度reinforce
开发语言·python
wuminyu2 小时前
JUC组件逐层剥离与深度剖析
java·linux·c语言·jvm·c++·算法
蓝斯4972 小时前
一碰即传,重构跨设备文件分享体验
开发语言·python·重构