preprocessor

How to convert an enum type variable to a string?

落花浮王杯 提交于 2019-11-26 01:44:31
问题 How to make printf to show the values of variables which are of an enum type? For instance: typedef enum {Linux, Apple, Windows} OS_type; OS_type myOS = Linux; and what I need is something like printenum(OS_type, \"My OS is %s\", myOS); which must show a string \"Linux\", not an integer. I suppose, first I have to create a value-indexed array of strings. But I don\'t know if that is the most beautiful way to do it. Is it possible at all? 回答1: There really is no beautiful way of doing this.

Is there a C pre-processor which eliminates #ifdef blocks based on values defined/undefined?

对着背影说爱祢 提交于 2019-11-26 00:55:42
问题 Original Question What I\'d like is not a standard C pre-processor, but a variation on it which would accept from somewhere - probably the command line via -DNAME1 and -UNAME2 options - a specification of which macros are defined, and would then eliminate dead code. It may be easier to understand what I\'m after with some examples: #ifdef NAME1 #define ALBUQUERQUE \"ambidextrous\" #else #define PHANTASMAGORIA \"ghostly\" #endif If the command were run with \'-DNAME1\', the output would be:

#ifdef replacement in the Swift language

一笑奈何 提交于 2019-11-25 23:23:39
问题 In C/C++/Objective-C you can define a macro using compiler preprocessors. Moreover, you can include/exclude some parts of code using compiler preprocessors. #ifdef DEBUG // Debug-only code #endif Is there a similar solution in Swift? 回答1: Yes you can do it. In Swift you can still use the "#if/#else/#endif" preprocessor macros (although more constrained), as per Apple docs. Here's an example: #if DEBUG let a = 2 #else let a = 3 #endif Now, you must set the "DEBUG" symbol elsewhere, though. Set

How to convert an enum type variable to a string?

会有一股神秘感。 提交于 2019-11-25 18:51:30
How to make printf to show the values of variables which are of an enum type? For instance: typedef enum {Linux, Apple, Windows} OS_type; OS_type myOS = Linux; and what I need is something like printenum(OS_type, "My OS is %s", myOS); which must show a string "Linux", not an integer. I suppose, first I have to create a value-indexed array of strings. But I don't know if that is the most beautiful way to do it. Is it possible at all? Bo Persson There really is no beautiful way of doing this. Just set up an array of strings indexed by the enum. If you do a lot of output, you can define an