Is there a way to reverse the order of a macro? - c

Hello (I apologise in advance if I'm wrong in the tittle),
I'm writing a program and all, I created a macro but I want to use it in a reverse order like:
#define MAGIC "\x34\x19\x23\x4C"
Would become something like :
"\x4C\x23\x19\x34"
Is there a way to do it or I need to write a new one?
Thanks in advance.

Preprocessor is responsible for macros (so they are interpreted before your program executes).
If you would like to use both "\x34\x19\x23\x4C" and "\x4C\x23\x19\x34" in your code it would be much better to declare a static global variable and then modify it by simple reverse function. Or, in fact, declare two macros.

Related

Reducing repetition in C

I haven't included any code because it's not important to my question, but say I have a statement like if (g_sound == 1){printf("\a");} (of course I am just using a shorter example), how could I use this code all over my program while not having so much repetitiveness? Is there some type of variable which you could link to and have the code in that variable executed? So that half my code isn't the same thing over and over again... All help is much appreciated!
UPDATE: Thank you all for your helpful comments, I figured out that using a function would be the most optimal since it is a 20 line code I need to have executed! Thanks once again!
If the statement is really that simple (or at least optimal) then use a macro:
#define THIS_IS_NOT_A_FUNCTION(X) if (x == 1){ printf ("\a");}
Then the macro is used like this:
// do some tedious task that I can't be bother typing out fully:
THIS_IS_NOT_A_FUNCTION(g_sound);
As David C. Rankin pointed out in his comment, macros are expanded to their defined value pre-compile time so don't have the overhead of calling a function. The downside is that the code can become unreadable if macros are used too liberally.
If the statement is a complex operation it may pay to write a function and call that.

#define for pedagogical purposes

I would like to hide some complexity from some non-trivial code I'm writing. Here, I would like to hide one level of indirection from a struct pointer, to make it more readable. I'm not asking whether this is clean or a best practice or not, I know it isn't, but I also know what I like to achieve :)
So, how kosher is to have something like
#define getmark() m->o->marked
besides that fact that I would not write it in non-academic code? That would allow me to do
n->getmark()
, which is nicer (and more to the point than)
n->m->o->marked
Is the #define code correct? Will it just do a text replace here with no other strings attached?
IMHO, most C programmers would prefer a function-style macro, like:
#define getmark(m) ((m) && (m)->o ? (m)->o->marked : -1)
Quite frankly - not.
You are not making your code eaiser to read, but instead hiding the fact that there is a hidden state machine (m pointer which points to o).
You also make this hack global - which might break if someone has such variables.
Also ... the trick of adding "()" after the macro to make it look like you are calling a normal function, instead of 2 indirections... is bad. It looks for a reader like there should be a function with this name, but instead you hide a monster (poker face....).
If you need to simplify the state machine, and you know that there will be only one state - create a global static variable and create plain functions to call those objects.
It will work, but even in C it's a terrible idea. Please don't do it.
If you want to avoid the 'source bloat' of repeated indirections, use a temporary pointer.
O* myO;
myO = n->m->o;
o->marked = o->this + o->that;
A problem with your original macro is that
n->getmark() = 123;
will work while it should not.

refer to a variable using multiple names in C

Is it possible to refer to a variable using multiple names in C ? I know this could be done via pointers but is it possible without using pointers. Just like we use 'typedef' for multiple naming of data type, similar for Variables
I have a constant named FILTER_PROC_LOAD_INTERNSITY, How to refer to it using simple name like 'var1'.
you may want to use macros?
#define var1 FILTER_PROC_LOAD_INTERNSITY
but the question is: why?
one "thing" one responsibility. You do not want to baffle the reader of your code. The name of the Variable seems to be wrong in the first place, if you have the need to rename the name.
Edith:
what my problem with readability is expressed in this example
char *very_ugly_variable_name;
#define beautifulVariableName very_ugly_variable_name
void unmaintainable_old_function() {
print(very_ugly_variable_name);
}
void myOtherNewFunction() {
print(beautifulVariableName);
}
you do not grok in a moment, that very_ugly_variable_name and beautifulVariableName are the exact same (in namescope and in memory).
The C language does not seem to have references (to alias your variable) but you can use a pointer to that end: yourtype* var1 = &FILTER_PROC_LOAD_INTERNSITY and then use *var1 to get the value of your constant. But this doesn't look like a good idea - symbolic names in programs are much easier to read and understand.

how can i declare variables via macros?

first of all, I'm using MS's Visual Studio and using C language.
Recently I need to declare variables with just one same statement which likes a macro.
However as you know, I can declare just one variable which have same name.
for example, this is not possible.
int iVar1;
int iVar1; // this is not possible.
so I thought about macros include __LINE__ , if I can use this predefined macro, I can declare lots of variables via just one macro statement.
But it was difficult to make.
I made macro like this.
#define MY_LINE_VARIABLE int g_iLine##__LINE__##Var = 0;
but after compile, i could get this variable named 'g_iLine_LINE_Var' instead of 'g_iLine123Var'
I want to know that is this possile, and how can i make it.
Furthermore, I need to use __FILE__ macro if possible. but this macro might be changed with string data. so I can not be sure.
Any advice will be helpful.
Thank you for your help in advance.
As #Chris Lutz has rightly said that, there might be a better way to accomplish what you want. Consider asking what you want to achieve.
But if you are just curious, this is the way to do:
#define var(z) int g_iLine##z##var = 0
#define decl(x) var(x)
#define MY_LINE_VARIABLE decl(__LINE__)
MY_LINE_VARIABLE;
MY_LINE_VARIABLE;
From this link :
After the preprocessor expands a macro name, the macro's definition
body is appended to the front of the remaining input, and the check
for macro calls continues. Therefore, the macro body can contain calls
to other macros.
So in your case :
MY_VARIABLE_LINE is converted to int g_iLine__LINE__Var;. But now __LINE__ is not a valid token anymore and is not treated as a predefined macro.
Aditya's code works like this:
MY_VARIABLE_LINE is converted to decl(__LINE__) which is converted to var(123) which is converted to int giLine123var = 0.
Edit: This is for GNU C

Variable names in C

In preprocessors, we can have switch between macros like,
#define BUFF(n) BUFF_##n
So, BUFF(1) would get replaced by BUFF_1, BUFF(2) would get replaced by BUFF_2 and song
Can this be applicable to C variables? i.e., choosing between similar variables dynamically. I understand it is a weird situation and can be handled using arrays or any other constructs.. but the situation demands me such situation.. could u plz help with this.. thanks in advance
Yes, you can use that macro to apply BUFF_ to just anything. The preprocessor will expand macros and then the compiler will try to compile the result. The latter might fail, since if you use BUFF(+) you get BUFF_+ and that's not a valid variable name.
Sure, you can do this. preprocessor macros are just text replacements that are done to the code before compilation. You can't do this during runtime, though.

Resources