stringification

Convert a preprocessor token to a string

筅森魡賤 提交于 2019-11-26 13:03:55
I'm looking for a way to convert a preprocessor token to a string. Specifically, I've somewhere got: #define MAX_LEN 16 and I want to use it to prevent buffer overrun: char val[MAX_LEN+1]; // room for \0 sscanf(buf, "%"MAX_LEN"s", val); I'm open to other ways to accomplish the same thing, but standard library only. Dan see http://www.decompile.com/cpp/faq/file_and_line_error_string.htm specifically: #define STRINGIFY(x) #x #define TOSTRING(x) STRINGIFY(x) #define AT __FILE__ ":" TOSTRING(__LINE__) so your problem can be solved by doing sscanf(buf, "%" TOSTRING(MAX_LEN) "s", val); I found an

C Preprocessor, Stringify the result of a macro

风格不统一 提交于 2019-11-26 11:18:16
I want to stringify the result of a macro expansion. I've tried with the following: #define QUOTE(str) #str #define TEST thisisatest #define TESTE QUOTE(TEST) And TESTE gets expanded to: "TEST", while I'm trying to get "thisisatest". I know this is the correct behavior of the preprocessor but can anyone help me with a way to achieve the other one? Using TESTE #TEST is not valid Using TESTE QUOTE(thisisatest) is not what I'm trying to do Like this: #include <stdio.h> #define QUOTE(str) #str #define EXPAND_AND_QUOTE(str) QUOTE(str) #define TEST thisisatest #define TESTE EXPAND_AND_QUOTE(TEST)

How, exactly, does the double-stringize trick work?

核能气质少年 提交于 2019-11-26 11:04:23
At least some C preprocessors let you stringize the value of a macro, rather than its name, by passing it through one function-like macro to another that stringizes it: #define STR1(x) #x #define STR2(x) STR1(x) #define THE_ANSWER 42 #define THE_ANSWER_STR STR2(THE_ANSWER) /* "42" */ Example use cases here . This does work, at least in GCC and Clang (both with -std=c99 ), but I'm not sure how it works in C-standard terms. Is this behavior guaranteed by C99? If so, how does C99 guarantee it? If not, at what point does the behavior go from C-defined to GCC-defined? Yes, it's guaranteed. It works

Concatenate int to string using C Preprocessor

谁说我不能喝 提交于 2019-11-26 09:19:11
问题 I\'m trying to figure out how I can concatenate a #define \'d int to a #define \'d string using the C Preprocessor. My compiler is GCC 4.1 on CentOS 5. The solution should also work for MinGW. I\'d like to append a version number onto a string, but the only way I can get it to work is to make a copy of the version number defines as strings. The closest thing I could find was a method of quoting macro arguments, but it doesn\'t work for #define s This is does not work. #define MAJOR_VER 2

Stringification - how does it work?

两盒软妹~` 提交于 2019-11-26 07:29:33
I know that: #define foo 4 #define str(s) #s with str(foo) writes out: "foo" , because stringify is executed first of text expansion, but this: #define xstr(s) str(s) #define str(s) #s #define foo 4 with xstr(foo) writes out: "4" . Why? What are the steps involved in the process? The relevant steps of macro expansion are (per C 2011 [n1570] 6.10.3.1 and C++ 1998 16.3.1): Process tokens that are preceded by # or ## . Apply macro replacement to each argument. Replace each parameter with the corresponding result of the above macro replacement. Rescan for more macros. Thus, with xstr(foo) , we

# and ## in macros

旧城冷巷雨未停 提交于 2019-11-26 06:36:54
问题 #include <stdio.h> #define f(a,b) a##b #define g(a) #a #define h(a) g(a) int main() { printf(\"%s\\n\",h(f(1,2))); printf(\"%s\\n\",g(f(1,2))); return 0; } Just by looking at the program one \"might\" expect the output to be, the same for both the printf statements. But on running the program you get it as: bash$ ./a.out 12 f(1,2) bash$ Why is it so? 回答1: Because that is how the preprocessor works. A single '#' will create a string from the given argument, regardless of what that argument

Pragma in define macro

橙三吉。 提交于 2019-11-26 03:08:12
问题 Is there some way to embed pragma statement in macro with other statements? I am trying to achieve something like: #define DEFINE_DELETE_OBJECT(type) \\ void delete_ ## type_(int handle); \\ void delete_ ## type(int handle); \\ #pragma weak delete_ ## type_ = delete_ ## type I am okay with boost solutions (save for wave) if one exists. 回答1: If you're using c99 or c++0x there is the pragma operator, used as _Pragma("argument") which is equivalent to #pragma argument except it can be used in

C Preprocessor, Stringify the result of a macro

我们两清 提交于 2019-11-26 02:21:31
问题 I want to stringify the result of a macro expansion. I\'ve tried with the following: #define QUOTE(str) #str #define TEST thisisatest #define TESTE QUOTE(TEST) And TESTE gets expanded to: \"TEST\", while I\'m trying to get \"thisisatest\". I know this is the correct behavior of the preprocessor but can anyone help me with a way to achieve the other one? Using TESTE #TEST is not valid Using TESTE QUOTE(thisisatest) is not what I\'m trying to do 回答1: Like this: #include <stdio.h> #define QUOTE

Stringification - how does it work?

只愿长相守 提交于 2019-11-26 01:58:32
问题 I know that: #define foo 4 #define str(s) #s with str(foo) writes out: \"foo\" , because stringify is executed first of text expansion, but this: #define xstr(s) str(s) #define str(s) #s #define foo 4 with xstr(foo) writes out: \"4\" . Why? What are the steps involved in the process? 回答1: The relevant steps of macro expansion are (per C 2011 [n1570] 6.10.3.1 and C++ 1998 16.3.1): Process tokens that are preceded by # or ## . Apply macro replacement to each argument. Replace each parameter

How do you convert a jQuery object into a string?

↘锁芯ラ 提交于 2019-11-25 23:39:10
问题 How do you convert a jQuery object into a string? 回答1: I assume you're asking for the full HTML string. If that's the case, something like this will do the trick: $('<div>').append($('#item-of-interest').clone()).html(); This is explained in more depth here, but essentially you make a new node to wrap the item of interest, do the manipulations, remove it, and grab the HTML. If you're just after a string representation, then go with new String(obj) . Update I wrote the original answer in 2009.