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;
}
相关推荐
luck_bor1 小时前
File类&递归作业
java·开发语言
枕星而眠4 小时前
数据结构八大排序详解(一):四大简单排序
c语言·数据结构·c++·后端
努力努力再努力wz4 小时前
【Qt入门系列】:按钮组件全解析:从 QAbstractButton 到快捷键事件、单选与复选机制
c语言·开发语言·数据结构·c++·git·qt·github
skywalk81635 小时前
言知(Yanzhi)系统提升建议报告和完工报告 by AutoCoder
开发语言·编程
yunn_5 小时前
单例模式两种实现方法
开发语言·c++·单例模式
我材不敲代码5 小时前
Python基础:列表详解、增删改查及常用高阶操作
开发语言·windows·python
AI玫瑰助手5 小时前
Python运算符:成员运算符(in/not in)的使用场景
开发语言·python·信息可视化
AI人工智能+电脑小能手6 小时前
【大白话说Java面试题 第77题】【Mysql篇】第7题:回表查询与全表扫描的区别?
java·开发语言·数据库·mysql·面试
水木流年追梦6 小时前
大模型入门-大模型分布式训练2
开发语言·分布式·python·算法·正则表达式·prompt
口袋里のInit7 小时前
基础知识——ARM M核入栈出栈流程
开发语言·arm开发