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;
}
相关推荐
1001101_QIA14 小时前
VRChat 插件开发方式
开发语言·vr
会编程的土豆16 小时前
GoWeb 处理请求详解:请求行、请求头、请求参数与给客户端响应
开发语言·http·golang
name好难取诶1 天前
PHP 静态分析工具实战 PHPStan 和 Psalm 完全指南
android·开发语言·php
湿滑路面1 天前
Rouyan:使用WPF/C#构建的基于LLM的快捷翻译小工具
开发语言·c#·wpf
Lee_jerome1 天前
《C/C++编译全家桶:g++四阶段、.a与.so区别、CMake构建、ARM交叉编译,这篇全讲透了》
c语言·开发语言·arm开发·c++·编译·cmake·cmakelists
kisloy1 天前
【爬虫入门第5讲】Python爬虫文本解析详解
开发语言·爬虫·python
浩浩kids1 天前
C•exercises
c语言
春生野草1 天前
个人笔记——C语言字符串、树
c语言·开发语言·笔记
风中芦苇啊1 天前
Java EasyExcel 导入通用工具类:自定义注解映射字段 + 反射机制
java·开发语言
PieroPc1 天前
Python 写的 Windows凭据添加工具 第二版 解决Win11 23H2, 25H2,26H1,共享文件和打印 一部份问题,不是全部!
开发语言·windows·python