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;
}
相关推荐
deepin_sir30 分钟前
10 - 函数
开发语言·python
z落落44 分钟前
C#String字符串
开发语言·c#·php
wljy11 小时前
二、进制状态转换
linux·运维·服务器·c语言·c++
猫头虎-前端技术1 小时前
JS 作用域与闭包:从变量提升到闭包陷阱的超详细解析
开发语言·javascript·云计算·bootstrap·ecmascript·openstack·perl
枫叶林FYL1 小时前
项目十:事件溯源仓储管理系统(WMS)仿真实现
开发语言·python
繁华落尽,倾城殇?2 小时前
[C++11] : atomic,nullptr,default/delete,enum class
开发语言·c++·c++11·nullptr·atomic·enum class·default/delete
01_ice2 小时前
C语言数据在内存中的存储
c语言·开发语言
代码村新手2 小时前
C++-二叉搜索树
开发语言·c++
bucenggaibian2 小时前
《C语言》编程前置:计算机底层逻辑(诞生的基础)
c语言·程序框架·编译运行·内存地址·底层逻辑
吃好睡好便好4 小时前
创建魔方矩阵和单位矩阵
开发语言·人工智能·学习·线性代数·matlab·矩阵