How do I define multiple #define with single assign value in C - c

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*/

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.

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)

Related

Is there a way to manipulate preprocessor define scope in C

Just curious. Imagine I need to have a #define A that is the sum of n numbers, and those n numbers have a meaning I'd like to make explicit, but only for the computation of A, to improve readiblity,
instead of writing n macros or just writing #define A <result_of_the_sum>.
Is there a way I could limit the reach of these n #define directives to just the definition of A ? Just as in C one would do :
int a = 0;
{
int b = 1;
int c = 2;
int d = 3;
a = b + c + d;
} // end of b,c,d scope.
My intention is to have A defined when compiling but no definition for the other n defines used to compute A, since these would only be useful to understand the code better.
Edit:
Imagine I have these macros:
#define MEANINGFUL_NUMBER_1 1
#define MEANINGFUL_NUMBER_2 2
#define MEANINGFUL_NUMBER_3 3
And I have a macro, A that is the sum of them, and I like someone reading my code to understand the value of A not just see it defined straightforwardly, i.e. #define A (MEANINGFUL_NUMBER_1 + ... + MEANINGFUL_NUMBER_N), such that only A is substituted before compilation but MEANINGUL_NUMBER_* is not.
Is there a way to manipulate preprocessor define scope in C
No.
Is there a way I could limit the reach of these n #define directives to just the definition of A ?
No.
Note that macros are evaulated upon use. That means that everything has to be visible when the macro is used. You can #undef all the macros after all usages, ergo "limit the reach" in that way.
In C, there are no namespaces. In C use prefixes. You would do:
#define LIB_PRIV_B 1
#define LIB_PRIV_C 2
#define LIB_PRIV_D 3
#define LIB_A (LIB_PRIV_B + LIB_PRIV_C + LIB_PRIV_D)
If you really do not want the numbers to leak into C, then use something to generate the C source code, which also gives you more power to the preprocessor side. Use jinja2, m4, php or python, and configure your build system to properly handle the generation dependency.

Sending parameter to a #define

I wonder to know is it possible to send a parameter to a #define macro for selecting different output
For example:
#define Row(1) LPC_GPIO0
#define Row(2) LPC_GPIO3
#define Row(3) LPC_GPIO2
Then in my code I create a loop for sending the parameter
Row(x)
This macro syntax doesn't exist.
Moreover, it can't possibly exist, because macros are expanded before the compiler compiles the code. If your x isn't a compile time constant, there would never be a way to determine what to replace in the source code for the macro invocation.
If you need to index some values, just use an array, e.g. (assuming these constants are integers):
static int rows[] = { 0, LPC_GPIO0, LPC_GPIO3, LPC_GPIO2 };
Writing
rows[x]
would have the effect you seem to have expected from your invalid macro syntax.
if you want to use macros
#define GPIOx(x) GPIO##x
and GPIOx(1) will expand to GPIO1
If you want these calculated at runtime there is a way to do what you want
#define Row(x) (x == 1 ? LPC_GPIO0 : (x == 2 ? LPC_GPIO3 : (x == 3 ? LPC_GPIO2 : ERROR_VALUE)))
Though this gets messy as the number of options increases
Also, even if you do want this evaluated at compile time, most optimizing compilers would do that for you as long as x is a constant

Can #define include previously defined variables?

How are the definitions in C processed? Are they processed in order of line numbers?
For example, will the following statements work?
#define ONE 1
#define TWO (ONE+1)
Could there be any problems with definitions that depend on previous definitions?
Yes, one #define can reference other #define substitutions and macros without any problem.
Moreover, the expression on these constants would remain a constant expression.
Your second expression would be textually equivalent to (ONE+1) replacement in the text, with no limits to the level of nesting. In other words, if you later define
#define THREE (TWO+1)
and then use it in an assignment i = THREE, you would get
i = ((ONE+1)+1)
after preprocessing.
If you are planning to use this trick with numeric values, a common alternative would be to use an enum with specific values, i.e.
enum {
ONE = 1
, TWO = ONE+1
, THREE = TWO+1
, ... // and so on
};
They're processed at point when they're used, so you example and even this
#define TWO (ONE+1)
#define ONE 1
will work.
The best way is to check by yourself:
g++ test.cpp
gcc test.c
For strict compiler check:
gcc test.c -pedantic
And all worked for me!
test.c/test.cpp
#include <stdio.h>
#define A 9
#define B A
int main()
{
printf("%d\n",B);
return 0;
}
The compiler processes the #define-s in the order they were de...fined. After each #define gets processed, the preprocessor then proceeds to process all text after this #define, using it in the state left by this #define. So, in your example:
#define ONE 1
#define TWO (ONE+1)
It first processes #define ONE 1, replacing all further occurunces of ONE with 1. So, the second macro becomes
#define TWO (1+1)
That is how it will be processed and applied by the preprocessor.
The reverse example:
#define TWO (ONE+1)
#define ONE 1
will also work. Why? Well, the preprocessor will take the first #define, scan the code for any occurences of TWO, and replace it with (ONE+1). Then it reaches the second #define, and replaces all occurences of ONE, including those put in place by the previous #define, with 1.
I'd personally prefer the former approach over the latter: it's plainly easier for the preprocessor to handle.

Iteration through defines

I'm working in a C program and I came across a problem. I have this
#define NUMBER_OF_OPTIONS 5
#define NAME_OPTION1 "Partida Rapida"
#define NAME_OPTION2 "Elige Nivel"
#define NAME_OPTION3 "Ranking"
#define NAME_OPTION4 "Creditos"
#define NAME_OPTION5 "Exit"
for (iterator = 1; iterator <= NUMBER_OF_OPTIONS; iterator++){
menu_options[iterator-1]= NAME_OPTION + iterator
}
I want that "NAME_OPTION + iterator" takes the value of the corresponding #define. For example if the variable "iterator" is equal to one, I want menu_options[iterator-1] to take the value of NAME_OPTION1, which is "Partida Rapida".
How can I get this?
Essentially, you can't. #define macros are handled by the C Preprocessor and do textual substitution wherever that macro appears in the code. The macro NAME_OPTION has not been defined, so the compiler should complain. C does not allow appending numbers onto strings, or especially onto symbols like NAME_OPTION. Use an array of const char*, which you can then refer to with your iterator.
You can't use defines as this, you can do:
const char *menu_options[5] = {
"Partida Rapida",
"Elige Nivel",
"Ranking",
"Creditos",
"Exit"
};
If you use #define macro, you just tell preprocessor to replace every occurence of defined word by something else before the code is compiled into machine code.
In this case NUMBER_OF_OPTIONS will be replaced by 5, but there's no occurence of NAME_OPTION*, so nothing will be replaced and you'll probably get an error while preprocessing.
Piere's solutions shows how to do it, but I highly doubt that there's an iterator over char *array, so you have to iterate over given array using an integer index.

use of #define in c

#include<stdio.h>
#include<stdlib.h>
#define d 10+10
int main()
{
printf("%d",d*d);
return 0;
}
I am new to the concept of macros.I found the output for the above program to be 120.What is the logic behind it?
Thanks.
10+10*10+10 == 10 + 100 + 10
Is that clear?
Macros are replaced literally. Think of search/replace. The compiler sees your code as 10+10*10+10.
It is common practice to enclose macro replacement texts in parentheses for that reason:
#define d (10 + 10)
This is even more important when your macro is a function-like macro:
#define SQ(x) ((x) * (x))
Think of SQ(a + b)...
d*d expands into 10+10*10+10. Multiplication comes before addition, so 10 + 100 + 10 = 120.
In general, #define expressions should always be parenthesized: #define d (10+10)
A macro is a nothing more than a simple text replacement, so your line:
printf("%d",d*d);
becomes
printf("%d",10+10*10+10);
You could use a const variable for more reliable behaviour:
const int d = 10+10;
The macro is expanded as is.
Your program becomes
/* declarations and definitions from headers */
int main()
{
printf("%d",10+10*10+10);
return 0;
}
and the calculation is interpreted as
10 + (10 * 10) + 10
Always use parenthesis around macros (and their arguments when you have them)
#define d (10 + 10)
#define
preprocessor directive substitute the first element with the second element.
Just like a "find and replace"
I'm not sure about #include but in C# #define is used at the top to define a symbol. This allows the coder to do things like
#define DEBUG
string connStr = "myProductionDatabase";
#if DEBUG
connStr = "myTestDatabase"
#edif
10+10*10+10 = 20 + 100 = 120
Simple math ;)
Macro doesn't evaluate the value (it doesn't add 10 + 10) but simply replaces all it's occurences with the specified expression.

Resources