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;
}
相关推荐
巧克力男孩dd6 分钟前
Python超典型练习题(第一次作业)
开发语言·python·算法
TlSfoward13 分钟前
TLSFOWARD TLS指纹
开发语言·数据库·爬虫·搜索引擎·https·php
闲猫1 小时前
LangChain / Core components / Models
开发语言·python·langchain
ComputerInBook2 小时前
c 和 c++ 中的宏块(macro)
c语言·c++··宏块·宏指令
-银雾鸢尾-2 小时前
C#中的索引器
开发语言·c#
bu_shuo2 小时前
c与cpp中的argc和argv
c语言·c++·算法
Qlittleboy3 小时前
PHP的接口参数传递的过多,max_input_vars = 5000
开发语言·php
Aurorar0rua3 小时前
CS50 x 2024 Notes Algorithms - 02
c语言·开发语言·学习方法
en.en..3 小时前
冒泡排序与选择排序完整对比解析
开发语言·数据结构·算法·c#·排序算法
兰令水3 小时前
hot100【acm版】【2026.7.18打卡-java版本】
java·开发语言·算法