stringification

C Macro Token Concatenation involving a variable - is it possible?

二次信任 提交于 2019-11-30 08:30:00
问题 I'm trying to define a macro to generate a token name, containing a variable. Basically, what I'm trying is this: #define GLUER(x,y,z) x##y##z #define PxDIR(x) GLUER(P,x,DIR) int main() { int port; port = 2; PxDIR(port) |= 0x01; } I'm hoping to generate the token P2DIR in the above statement, but according to my compiler output, it's generating the token PportDIR, which is NOT what I wanted. Any help here? Or is what I'm attempting to do impossible? 回答1: I don't think what you're trying to do

C Preprocessor generate macros by concatenation and stringification [duplicate]

烈酒焚心 提交于 2019-11-29 12:27:01
This question already has an answer here: What are the applications of the ## preprocessor operator and gotchas to consider? 13 answers I have a set of target macros for which I want to generate aliases based on a choosing macro, like so: Choosing macro: #define I2C_MODULE 1 Alias macros (conceptual form): #define I2C_MODULE_BASE I2C<Value of I2C_MODULE>_BASE #define I2C_MODULE_NVIC INT_I2C<Value of I2C_MODULE> Target macros (from an external file out of my control): #define INT_I2C0 24 #define INT_I2C1 53 ... #define I2C0_BASE 0x40020000 #define I2C1_BASE 0x40021000 ... I wanted to have the

Why can't you stringify a function expression?

浪子不回头ぞ 提交于 2019-11-29 12:06:05
问题 Why doesn't this produce anything? console.log(JSON.stringify(function(){console.log('foobar');})); 回答1: JSON can't stringify functions at all, it handles them just like undefined or null values. You can check the exact algorithm at EcmaScript 5.1 §15.12.3, see also the description at MDN. However you of course can stringify function expression by casting them to a string, try console.log("" + function(){console.log('foobar');}) 回答2: JSON has no means to represent a function. It is a data

Stringify C preprocess

♀尐吖头ヾ 提交于 2019-11-29 08:12:49
This is my first post, so if I'm being too vague or giving information that everyone would intuitively assume, please let me know. I'm very new to writing in C and am just trying to get a better understanding of preprocessing. I'm writing a simple program that can take in arguments either directly from the console using gcc -Wall -std=c99 -DSEED=argument , where my argument should be a an integer, or if the -D is not defined the user will input it. The SEED value is simply used in srand() . I'm very confused why my code will not compile if I put in an -DSEED=a as my argument while if I put

How to single-quote an argument in a macro?

假如想象 提交于 2019-11-29 06:31:32
I would like to create a C pre-processor macro that will single-quote the argument. Just like the common used #X . I want Q(A) to be expanded to 'A' . I am using gcc on Linux. Does any one have an idea? I know # double-quotes. I am looking for a similar mechanism for single-quoting. The best you can do is #define Q(x) ((#x)[0]) or #define SINGLEQUOTED_A 'A' #define SINGLEQUOTED_B 'B' ... #define SINGLEQUOTED_z 'z' #define Q(x) SINGLEQUOTED_##x This only works for a - z , A - Z , 0 - 9 and _ (and $ for some compilers). Actually, #X double quotes its argument, as you can see with the following

Evaluate preprocessor token before ## concatenation

北城以北 提交于 2019-11-28 07:17:50
问题 I would like to evaluate a token before it is concatenated with something else. The "problem" is that the standard specifies the behaviour as before the replacement list is reexamined for more macro names to replace, each instance of a ## preprocessing token in the replacement list (not from an argument) is deleted and the preceding preprocessing token is concatenated with the following preprocessing token. hence in the following example, #include <stdlib.h> struct xy { int x; int y; };

C Preprocessor generate macros by concatenation and stringification [duplicate]

半城伤御伤魂 提交于 2019-11-28 06:44:08
问题 This question already has answers here : What are the applications of the ## preprocessor operator and gotchas to consider? (13 answers) Closed 4 years ago . I have a set of target macros for which I want to generate aliases based on a choosing macro, like so: Choosing macro: #define I2C_MODULE 1 Alias macros (conceptual form): #define I2C_MODULE_BASE I2C<Value of I2C_MODULE>_BASE #define I2C_MODULE_NVIC INT_I2C<Value of I2C_MODULE> Target macros (from an external file out of my control):

Stringifying template arguments

本小妞迷上赌 提交于 2019-11-28 04:07:46
Is it possible in C++ to stringify template arguments? I tried this: #define STRINGIFY(x) #x template <typename T> struct Stringify { Stringify() { cout<<STRINGIFY(T)<<endl; } }; int main() { Stringify<int> s; } But what I get is a 'T', and not an 'int'. Seems that the preprocessors kicks in before template resolution. Is there any other way to do this? Is there any way for the preprocessing to take place after template resolution? (Compiler is VC++). You could try typeid(T).name() Edit : Fixed based on comments. Cat Plus Plus You could use some template magic. #include <iostream> template

What are some tricks I can use with macros? [closed]

馋奶兔 提交于 2019-11-28 02:54:14
In our legacy code, as well as our modern code, we use macros to perform nifty solutions like code generations, etc. And we make use of both the # and ## operators. I am curious how other developers use macros to do cool things, if they use them at all. In C, it's common to define macros that do some stuff getting the verbatim argument, and at the same time define functions to be able to get the address of it transparently. // could evaluate at compile time if __builtin_sin gets // special treatment by the compiler #define sin(x) __builtin_sin(x) // parentheses avoid substitution by the macro

Stringify C preprocess

我怕爱的太早我们不能终老 提交于 2019-11-28 01:39:56
问题 This is my first post, so if I'm being too vague or giving information that everyone would intuitively assume, please let me know. I'm very new to writing in C and am just trying to get a better understanding of preprocessing. I'm writing a simple program that can take in arguments either directly from the console using gcc -Wall -std=c99 -DSEED=argument , where my argument should be a an integer, or if the -D is not defined the user will input it. The SEED value is simply used in srand() . I