Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C Macro trick to hide assignment?

Tags:

c

macros

I have a project with a handful of files: source1.c source2.c source1.h source2.h. Now source1.c declares some variables and source1.h externs them (with a conditional build macro). source2.c would then use the extern variables by assigning a value to them.

The issue is that source1.h has something like the following:

#ifdef CUST1
extern type var_name;
#else
// Need to extern something or use a clever macro here but i dont like these below
//#define var_name int x (requires top of scope block usage)
//#define var_name // (doesnt compile)
//#define var_name -1 (simply doesnt compile)
//#define var_name 1= (This seems like it would reduce to unused 1==1; but doesnt work)
#endif

My build works for CUST1 but it will not work for CUST2 because var_name is never declared/outta scope when it is referenced in source2.c.

I dont want to use var_name for CUST2 and it is unreachable code in source2.c. Here is my question, how can i use a macro to #define var_name so that the assignment "goes away" or does nothing?

I could "#define var_name int x". This would put int x on the stack in the source2.c functions and assign the values in source2.c, but if it is ever referenced anywhere besides the top of a scope block, my old (C89?) compiler will error.

For example if source2.c ever had the following it would fail to compile:

unsigned short local_var = 0;
local_var = 1; //or someother value
var_name = local_var * 2;

I could wrap the logic in source2.c with the same #ifdef CUST1 macros but that seems not so good either.

If var_name was only being compared against it would not be so bad because i could just use #define var_name -1 or something that would fail all compares/switches. The issues is -1 = temp_var; does not compile because -1 cant be an lvalue.

Similiarly i cant "#define var_name //" because comments are removed before the marcos are replaced according to this:Can you #define a comment in C?

Is there a clever macro trick that will hide/remove this assignment, that does not put a local_var on the stack? I feel like there is something possible with a ternary but i cant think of it.

EDIT minimal example code:

source1.c

int var_name = 0;

source1.h

#ifdef CUST1
extern int var_name;
#else
// clever macro
#endif

source2.c

#include "source1.h"
int main(){
  var_name = 1;
  return 0;
}
like image 339
Bwebb Avatar asked Feb 18 '26 15:02

Bwebb


1 Answers

If the code accessing var_name in source2.c is unreachable when CUST1 is not defined (as you seem to say), then you can define it like this:

#include <stdlib.h>

#define var_name (*(abort(), (type *)0))

This will work syntactically as an lvalue (or rvalue) of the correct type, but will abort the program if it's actually ever executed.

like image 183
caf Avatar answered Feb 21 '26 09:02

caf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!