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;
}
相关推荐
紫金修道1 天前
【DeepAgent】概述
开发语言·数据库·python
Via_Neo1 天前
JAVA中以2为底的对数表示方式
java·开发语言
书到用时方恨少!1 天前
Python multiprocessing 使用指南:突破 GIL 束缚的并行计算利器
开发语言·python·并行·多进程
cch89181 天前
PHP五大后台框架横向对比
开发语言·php
天真萌泪1 天前
JS逆向自用
开发语言·javascript·ecmascript
野生技术架构师1 天前
一线大厂Java面试八股文全栈通关手册(含源码级详解)
java·开发语言·面试
Q一件事1 天前
R语言制图-相关性及关系网络图
开发语言·r语言
坊钰1 天前
Java 死锁问题及其解决方案
java·开发语言·数据库
551只玄猫1 天前
【数学建模 matlab 实验报告1】
开发语言·数学建模·matlab·课程设计·实验报告
三道渊1 天前
C语言:文件I/O
c语言·开发语言·数据结构·c++