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;
}
相关推荐
geovindu15 小时前
python: Generators Pattern
开发语言·python·设计模式·生成器模式
wuminyu15 小时前
Java锁膨胀机制之偏向锁到轻量级锁源码剖析
java·linux·c语言·jvm·c++
没有不重的名么15 小时前
spyder使用教程
开发语言·python
阿正的梦工坊15 小时前
【Rust】06-函数、控制流与模块组织
开发语言·算法·rust
十月的皮皮15 小时前
C语言学习笔记20260611-水仙花数(2种解法)
c语言·笔记·学习
狗凯之家源码网15 小时前
永夜大圣 H5 棋牌大厅源码效果实测与品质解析
java·开发语言
爱装代码的小瓶子15 小时前
muduo库 --socket的封装
服务器·开发语言·php
凡人叶枫15 小时前
Effective C++ 条款13:以对象管理资源(RAII)
java·linux·开发语言·c++·嵌入式开发
Cloud_Shy61815 小时前
解读《Effective Python 3rd Edition》:从练气到老魔(第五章 Item 36 - 39)
开发语言·人工智能·笔记·python
阿正的梦工坊15 小时前
【Rust】11-Rust 所有权模型的编译期推理机制
开发语言·算法·rust