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;
}
相关推荐
c2385615 分钟前
c/c++中的多态(上)
开发语言·c++
彷徨而立18 分钟前
【C++】介绍 std::ifstream 输入文件流
开发语言·c++
罗超驿33 分钟前
13.JavaScript 新手入门指南:语法、变量、流程控制全解析
开发语言·javascript
yingjie11038 分钟前
Scanpy vs Seurat 深度对比:Python 与 R 的单细胞分析框架谁更强?
开发语言·python·r语言·生物信息学·单细胞转录组·seurat·scanpy
程序大视界1 小时前
【C++ 从基础到项目实战】C++(六):拷贝控制——浅拷贝与深拷贝,兼谈智能指针
开发语言·c++·cpp
luck_bor1 小时前
IO流知识点笔记
java·开发语言·笔记
程序大视界1 小时前
【Python系列课程】Pandas(四):数据统计与排序——describe、sort_values、sample
开发语言·python·pandas
KWTXX1 小时前
使用matlab官网的skills调用claude-待完成
开发语言·matlab
Cthy_hy2 小时前
Python算法竞赛:排列组合核心用法
开发语言·python·算法