经典代码-C宏 #转字符串【瓦特芯 笔记】
在调试C语言程序时,有时需要打印宏的名字。可以通过定义宏,宏名字的数组来获得。
例如:
- #include <stdio.h>
- #define MACRO_STR(x) {x, #x}
- typedef struct _macro_str {
- int id;
- char *name;
- }MACRO_STR_T;
- typedef enum _color{
- RED,
- GREEN,
- BLUE,
- }COLOR;
- MACRO_STR_T g_color_str[] ={
- MACRO_STR(RED),
- MACRO_STR(GREEN),
- MACRO_STR(BLUE),
- {-1, NULL}
- };
- static const char * get_macro_name(MACRO_STR_T* table, int id)
- {
- int i = 0;
- while(table[i].id != -1 && table[i].name != NULL){
- if(table[i].id == id)
- return table[i].name;
- i++;
- }
- return "";
- }
- static const char * get_color_name(COLOR color)
- {
- return get_macro_name(g_color_str, color);
- }
- int main()
- {
- COLOR color = RED;
- printf("The name of color %d is '%s'. \n", color, get_color_name(color));
- return 0;
- }
经典代码-C宏 #转字符串【瓦特芯 笔记】的更多相关文章
- CCS 5 XDS100 仿真连接错误Error connecting to the target【瓦特芯笔记】
问题现象:在点击仿真是出现连接错误: Error connecting to the target: (Error -151 @ 0x0) One of the FTDI driver funct ...
- C++中使用函数指针 【瓦特芯笔记】
在C++类中使用函数指针. 类型定义: typedef 返回类型(类名::*新类型)(参数表) //类定义 class CA { public: char lcFun(int a) ...
- javascript 写一段代码,判断一个字符串中出现次数最多的字符串,并统计出现的次数
javascript 写一段代码,判断一个字符串中出现次数最多的字符串,并统计出现的次数 function test(){ var bt = document.getElementById(" ...
- js让菜单栏一直悬浮在顶部,经典代码
js让菜单栏一直悬浮在顶部,经典代码 很简单,你只需要把下面代码放到js中:$(function(){ //获取要定位元素距离浏览器顶部的距离 var na ...
- ios宏定义字符串
ios宏定义字符串 #define objcString(str) @""#str"" 使用效果: objcString(字符串)
- java代码中fastjson生成字符串和解析字符串的方法和javascript文件中字符串和json数组之间的转换方法
1.java代码中fastjson生成字符串和解析字符串的方法 List<TemplateFull> templateFulls = new ArrayList<TemplateFu ...
- Javascript中最常用的61段经典代码
1. oncontextmenu="window.event.returnValue=false" 将彻底屏蔽鼠标右键<table border oncontextmenu= ...
- PHP之cookie相关实例教程与经典代码
·php 中cookie和session的用法比较 ·php会话控制cookie与Session会话处理 ·php中利用cookie实现购物车实例 ·php中cookie与session应用学习笔记 ...
- [改善Java代码]推荐在复杂字符串操作中使用正则表达式
一.分析 字符串的操作,诸如追加.合并.替换.倒序.分隔等,都是在编码过程中经常用到的,而且Java也提供了append.replace.reverse.split等方法来完成这些操作,它们使用起来 ...
随机推荐
- Akka的Actor模型及使用实例
本文的绝大部分内容转载自rerun.me这一blog,老外写的东西就是好啊. ACTORS介绍 Anyone who has done multithreading in the past won't ...
- Codeforces Round #269 (Div. 2)
A 题意:给出6根木棍,如果有4根相同,2根不同,则构成“bear”,如果剩余两个相同,则构成“elephant” 用一个数组分别储存各个数字出现的次数,再判断即可 注意hash[i]==5的时候,也 ...
- android调用JPush获取手机的注册码(Cordova环境)
JPushInterface.addLocalNotification(cordova.getActivity().getApplication().getApplicationContext(), ...
- C# winform 窗体从右下角向上弹出窗口效果
参考自 http://blog.csdn.net/yilan8002/article/details/7197981 /// <summary> /// 窗体动画函数 注意:要引用Syst ...
- LeetCode Factorial Trailing Zeroes (阶乘后缀零)
题意:如标题 思路:其他文章已经写过,参考其他. class Solution { public: int trailingZeroes(int n) { <? n/: n/+trailingZ ...
- Oracle中将小数转换成字符丢零.截取小数.除数为零解决法
如下所示,前面少个0 SQL>select money from users where username ='LEI'; money --------- .3256 解决方法: SQL> ...
- iOS - NSLog、UncaughtException日志保存到文件
转:http://blog.csdn.net/marujunyy/article/details/12005767 对于真机,日志没法保存,不好分析问题.所以有必要将日志保存到应用的Docunment ...
- hdu 1198 Farm Irrigation
令人蛋疼的并查集…… 我居然做了大量的枚举,居然过了,我越来越佩服自己了 这个题有些像一个叫做“水管工”的游戏.给你一个m*n的图,每个单位可以有11种选择,然后相邻两个图只有都和对方连接,才判断他们 ...
- PHP 获取远程文件的大小的3种方法
1.使用file_get_contents() <?php $file = file_get_contents($url); echo strlen($file); ?> 2. 使用get ...
- C++继承与派生的概念、什么是继承和派生
在C++中可重用性是通过继承(inheritance)这一机制来实现的.因此,继承是C++的一个重要组成部分. 前面介绍了类,一个类中包含了若干数据成员和成员函数.在不同的类中,数据成员和成员函数是不 ...