stringification

Interpreting C pre pre processor code

自古美人都是妖i 提交于 2021-02-16 14:54:26
问题 Given #define LOG_OBJECT(object) (NSLog(@"" #object @" %@ %@:%d”, [object description], [[NSString stringWithUTF8String:__FILE__] lastPathComponent], __LINE__)); The statement LOG_OBJECT(self); will produce: 2014-07-18 17:43:30.041 FrogCamera[12484:2144843] self ViewController.m:20 I wish to understand how the preprocessor code works. How can I see the statement that the pre processor produced? Specifically: Why the whole #define statement is wrapped in ( ) ? Is #object a text replacement for

C Preprocessor stringification (again) [duplicate]

核能气质少年 提交于 2021-02-08 06:34:49
问题 This question already has answers here : Can the C preprocessor perform integer arithmetic? (6 answers) Closed 1 year ago . Is it possible to have stringification after numeric evaluation? This is better explained with a simple example: #define A 1 #define B 2 #define SUM (A + B) #define STR_IMPL_(x) #x #define STR(x) STR_IMPL_(x) char *sum = STR(SUM); As written this generates the single line: char *sum = "(1 + 2)"; Is it possible, somehow, to generate char *sum = "3"; instead? I suspect

C Preprocessor: Stringify int with leading zeros?

旧时模样 提交于 2021-02-07 18:27:27
问题 I've seen this topic which describes the "stringify" operation by doing: #define STR_HELPER(x) #x #define STR(x) STR_HELPER(x) #define MAJOR_VER 2 #define MINOR_VER 6 #define MY_FILE "/home/user/.myapp" STR(MAJOR_VER) STR(MINOR_VER) Is it possible to stringify with leading zeros? Let's say my MAJOR_REV needs to be two characters "02" in this case and MINOR_REV 4 characters "0006" If I do: #define MAJOR_VER 02 #define MINOR_VER 0006 The values will be treated as octal elsewhere in the

C Preprocessor: Stringify int with leading zeros?

…衆ロ難τιáo~ 提交于 2021-02-07 18:27:26
问题 I've seen this topic which describes the "stringify" operation by doing: #define STR_HELPER(x) #x #define STR(x) STR_HELPER(x) #define MAJOR_VER 2 #define MINOR_VER 6 #define MY_FILE "/home/user/.myapp" STR(MAJOR_VER) STR(MINOR_VER) Is it possible to stringify with leading zeros? Let's say my MAJOR_REV needs to be two characters "02" in this case and MINOR_REV 4 characters "0006" If I do: #define MAJOR_VER 02 #define MINOR_VER 0006 The values will be treated as octal elsewhere in the

Preprocessor macro value to Objective-C string literal

 ̄綄美尐妖づ 提交于 2020-12-27 08:38:30
问题 I have a preprocessor macro defined in build settings FOO=BAR That value I want to massage into an Objective-C string literal that can be passed to a method. The following #define does not work, but it should demonstrate what I am trying to achieve: #define FOOLITERAL @"FOO" //want FOOLITERAL to have the value of @"BAR" myMethodThatTakesAnNSString(FOOLITERAL); I expect that I am just missing the obvious somehow, but I cannot seem to find the right preprocessor voodoo to get what I need. 回答1:

Replace a string in a macro variable?

雨燕双飞 提交于 2020-06-26 13:51:26
问题 I have a macro where I pass in an argument and use that define a new variable based on the name of the input: #define DO_X(x) char _do_x_var_ ## x; /* other things */ The problem is if I pass in a struct variable, it breaks: DO_X(some_struct->thing) becomes: char _do_x_var_some_struct->thing; /* other things */ EDIT: What I want it to evaluate to is: char _do_x_var_some_struct__thing; /* other things */ (or any valid variable name containing something similar to the input) What I actually

关于C语言中的连接符的用法 # ## macro stringize

蓝咒 提交于 2020-05-02 10:51:35
首先面向需求,小工程、代码不多、版本还挺多... 打算用宏控制开关选项、然后通过一长串 #if #elif ... #endif 来控制生成版本号、而且还要根据兼容性跟细小变更做三级版本号.... 所以就需要一个版本号拼接、明显,应该是编译期能做完的事情。 --------需求完毕-------- C语言宏定义应该是可完成的、在代码执行期间更加是没问题的。 虽然用string.h也可以完成拼接。 不舒服斯基... 初步查找 使用 # 和##,可以完成、进一步查找发现有个#@ 是什么鬼... 部分编译器不支持 经过初步试用# 会直接把传递进来的macro直接变成string,#@理论上是实现我需要的功能,然而并不支持 ---------------神奇的分割线------------------------- 进一步查找stringize site:gcc.gnu.org 找到页面 3.4 Stringification https://gcc.gnu.org/onlinedocs/cpp/Stringification.html 鉴于是blog,就不贴原文了,又需要的自己去看。 结论: If you want to stringify the result of expansion of a macro argument, you have to use two levels of

How to stringify a string which contains a comma?

╄→尐↘猪︶ㄣ 提交于 2020-01-22 16:00:29
问题 I want to pass a version string in the compile command: $ g++ -Wall -D VERSION="2013-12-03 02:15:21, commit cb060df" -o main main.cpp Inside my code, I have the following: #define TOSTR_(x) #x #define STRINGIFY(x) TOSTR_(x) #define VERSION_STR STRINGIFY(VERSION) This doesn't work, because the VERSION macro has a comma in it, so it looks like I'm passing two arguments to TOSTR() (apparently, the VERSION macro only gets expanded after it's passed to STRINGIFY() as one unique argument). The

Is there a way to use C++ preprocessor stringification on variadic macro arguments?

喜欢而已 提交于 2020-01-09 07:35:49
问题 My guess is the answer to this question is no, but it would be awesome if there was a way. To clarify, assume I have the following macro: #define MY_VARIADIC_MACRO(X...) // Does some stuff here in the macro definition What I would like to do is somehow perform stringification on all the variables of X before passing it to a variadic function; the keyword here is before. I realize there's no way to really access the individual arguments from within the macro definition, but is there a way to

Is there a way to use C++ preprocessor stringification on variadic macro arguments?

六月ゝ 毕业季﹏ 提交于 2020-01-09 07:35:30
问题 My guess is the answer to this question is no, but it would be awesome if there was a way. To clarify, assume I have the following macro: #define MY_VARIADIC_MACRO(X...) // Does some stuff here in the macro definition What I would like to do is somehow perform stringification on all the variables of X before passing it to a variadic function; the keyword here is before. I realize there's no way to really access the individual arguments from within the macro definition, but is there a way to