2

I had found some related question about How do I #define multiple values C / C++. But that is not what I am looking for. Instead, I want to do the opposite way.

We can simply assign something like this

int a,b,c,d,e;
a = b = c = d = e = 5;
printf("Value %d",e);

But what I am looking for is how to combine below define into one line, given fix value to assign.

#define SIZE_OF_NODE_ADD   4
#define SIZE_OF_KEY_ADD    4
#define SIZE_OF_ID_ADD     4
#define SIZE_OF_PW_ADD     4

Is it possible to do that ? something like

#define SIZE_OF_NODE_ADD = SIZE_OF_KEY_ADD = SIZE_OF_ID_ADD = SIZE_OF_PW_ADD = 4
/* The syntax is wrong, I just emphasize for easier understanding what I am looking for*/  
JDC
  • 43
  • 6
  • 1
    What is your actual objective? Why is "one line" so important? If it is to avoid repeating the constant then just have a single define of that constant and then all the other defines would use that first one. `#define MY_CONST 4` then `#define SIZE_OF_NODE_ADD MY_CONST` – kaylum Jun 17 '20 at 04:18
  • Objective clean up line of code and hope learn something can make the code prettier – JDC Jun 17 '20 at 04:25
  • 1
    "Objective" means not based on opinion. It would be more correct to say "subjective" as some would be of the opinion that trying to cram all those defines onto one line would be harder to read and not an improvement even if it could be done. – kaylum Jun 17 '20 at 04:28
  • Okay. Because when I read the code and saw multiple #define with same value, my mind was thinking that is it possible/got any idea to combine it. Imagine when u scroll down 30 lines doing the same thing just the different #define name. – JDC Jun 17 '20 at 04:32
  • 1
    That won't clean up the code - it will make it less maintainable. If you move to a different architecture where one of the nodes is a different size, then you will have to undo your *clean up*, reinstate the old code and change the node that is a different size. You are just making it harder for the next person maintaining the code – cup Jun 17 '20 at 05:20
  • If the numbers happen to be sizes of data types, I'd rather write that like `#define SIZE_OF_XXX sizeof (uint32_t)`. – the busybee Jun 17 '20 at 05:47
  • You'd have to really convince me that you need to define any macros. Doesn't a `static const int SIZE_OF_NODE_ADD = 4` work? Or, if you need those to be the classic compile time constants, doesn't the `enum { SIZE_OF_NODE_ADD = 4 };` work? Why are you using preprocessor? – Kuba hasn't forgotten Monica Jun 17 '20 at 06:24
  • Monica, the preprocessor is come from previous owner. It had been use in many place. I just read the code and somehow thinking of is it possible to combine. Then I search across and post to here for advice and answer. Seem like my thinking of 'combination' will make the code more complicated and less maintenance. – JDC Jun 17 '20 at 06:42
  • Related: [Assign two C preprocessor macros in the same command](https://stackoverflow.com/questions/40450733/assign-two-c-preprocessor-macros-in-the-same-command) – RobertS supports Monica Cellio Jun 17 '20 at 08:42
  • @JDC Well, OK. Revised my decision as you are actually asking for how to define multiple macros at the same time, not to assign them. I retract my duplicate vote but mark it as "Related". – RobertS supports Monica Cellio Jun 17 '20 at 08:42

2 Answers2

4

Well, #define statements can handle multiple lines using '\' at the end of the line without any other character next, so you can create fancy MACROS (Multi-line DEFINE directives?). But a simple solution for your problem may be using a reference definition so you just have to change that one to change the others value.

#define REFERENCE_VALUE 4

#define SIZE_OF_NODE_ADD   REFERENCE_VALUE
#define SIZE_OF_KEY_ADD    REFERENCE_VALUE
#define SIZE_OF_ID_ADD     REFERENCE_VALUE
#define SIZE_OF_PW_ADD     REFERENCE_VALUE

Remember that the REFERENCE_VALUE has to be before its use in the file, otherwise it won't compile.

1

It's in the C standard, that a preprocessor statement is terminated by a new line, so you must do:

#define SIZE_OF_NODE_ADD   4
#define SIZE_OF_KEY_ADD    4
#define SIZE_OF_ID_ADD     4
#define SIZE_OF_PW_ADD     4

A multiple define statement is not allowed.(And even really hard to read)

Mike
  • 4,041
  • 6
  • 20
  • 37