I have following
#define COMPANY ABC // should be happen by calling the script via Argument
#ifdef COMPANY
#define COMPANY // -> now ABC should be defined
#endif
#if defined (ABC)
// ...
#elif defined (DEF)
// ...
#else
// ...
#endif
My idea was:
COMPANY is defined, value is "ABC"
#ifdef COMPANY becomes true, so the next define will be executed
#define COMPANY will be replaced by #define ABC because of the definition of COMPANY itself
OK, it doesn't work because the preprocessor doesn't replace its own definitions...
Is there a possibility to do this?
Is there a possibility to do this?
No, it is not possible to do dynamically in a ny way. The token after #define is taken literally and is not subject to expansions. It is not possible to define a macro based on computation of other macro.
Typically, enumerations and choices are done as following:
#define COMPANY ABC // should be happen by calling the script via Argument
#define ABC 1
#define DEF 2
#if COMPANY == ABC
// for company ABC
#elif COMPANY == DEF
// for company DEF
#else
#error COMPANY has to be ABC or DEF
#endif
Related
#if defined(_WIN32)
#if !defined(_CRT_SECURE_NO_WARNINGS)
#define _CRT_SECURE_NO_WARNINGS
#endif
So the above is the first 4 lines of C language code from a file of some project.
I do know that #if defined (macro name) means that if the macro is defined, the value of the expression is 1 and if not, then it's 0.
So basically, the first two lines will be either 1 or 0 but what would they do? 1 or 0 just sitting alone?
It's defined() which will be evaluated to either 0 or 1 and from that value if will decide if it should go to the body. It's like normal if statement.
So basically, the first two lines will be either 1 or 0 but what would they do? 1 or 0 just sitting alone?
#if expression
won't result in a line replaced by the result of expression but the preprocessor will decide upon the value of expressen in
#if expression
controlled text
#endif /* expression */
if it will process the controlled text or not.
So basically, the first two lines will be either 1 or 0 but what would they do? 1 or 0 just sitting alone?
No, the #if would stay, just the defined() would become 0 or 1.
So assuming for example _WIN32 and _CRT_SECURE_NO_WARNINGS were defined, then the 1st two line you show looked like:
#if 1
#if 0
Those two if-statements control if what is following is taken into account until the next #endif statement.
The above snippet though is incomplete and should look like this:
#if 1
#if 0
#define FOO 42
#endif
#endif
The above would not #define FOO.
The below would not #define FOO as well:
#if 0
#if 1
#define FOO 42
#endif
#endif
The following will define FOO:
#if 1
#if 1
#define FOO 42
#endif
#endif
Back to your example code:
The stuff "controlled" by the outer if-statement
#if defined(_WIN32)
is
#if !defined(_CRT_SECURE_NO_WARNINGS)
#define _CRT_SECURE_NO_WARNINGS
The stuff "controlled" by the inner if-statement
#if !defined(_CRT_SECURE_NO_WARNINGS)
is
#define _CRT_SECURE_NO_WARNINGS
I have defined a macros as shown below.
#define NAME_OUT(name_in) PRE_##name_in##_POST
I would like to iterate through this macro using names i have defined in a table/array. Is it possible to do something like this? If so how would I do this?
NOTE: The above example is for illustrative purposes only :)
It's not entirely clear what you are asking, but it sounds a lot like you are looking for the "X macros" pattern:
#include <stdio.h>
// list of data
#define NAME_LIST \
X(foo) \
X(bar) \
X(hello) \
X(world)
// whatever you are actually using these for, maybe an enum or variable names?
typedef enum
{
// temporarily define the meaning of "X" for all data in the list:
#define X(name) PRE_##name##_POST,
NAME_LIST
#undef X // always undef when done
} whatever_t;
// helper macro to print the name of the enum
#define STRINGIFY(str) #str
int main()
{
#define X(name) printf("%s %d\n", STRINGIFY(PRE_##name##_POST), PRE_##name##_POST);
NAME_LIST
#undef X
}
Output:
PRE_foo_POST 0
PRE_bar_POST 1
PRE_hello_POST 2
PRE_world_POST 3
So my Visual studio is declaring both tag1 and tag2 as undefined, but they are cleary defined, can't i define one based on the other?
#define push 99
#define last_instruction push
#ifdef DEBUG
#define new_instr (1+last_instruction) //should be 100
#undef last_instruction
#define last_instruction new_instr //redifine to 100 if debug
#endif
I have some cases with tag2 and it says that the definition must be const, but it is constant it is 1+99, any help would be appreciated.
Thanks!
BA
First of all, you can't define the same macro twice. If you need to replace a macro, you first have to #undef it:
#define tag1 99
#ifdef DEBUG
#define tag2 (1+tag1)
#undef tag1
#define tag1 tag2
#endif
But this will not solve the problem. Macros are not variables, you can't use them to store values to re-use at a later point. They are text replacement, so they sort of exist in parallel.
So the new definition #define tag1 tag2 expands to 1+tag1. But at this point, there is nothing called tag1, because we just undefined it and we are not yet done re-defining it.
Ponder this too much and you'll turn crazy :) So just forget about that whole thing, what you really want to do is this:
#define tag1_val 99
#define tag1 tag1_val
#ifdef DEBUG
#undef tag1
#define tag1 (tag1_val+1)
#endif
If all you want is a few symbolic names for integer constants, you could define them in an enum like this:
enum {
push = 99,
#ifdef DEBUG
new_instr,
#endif
last_plus_1,
last_instr = last_plus_1 - 1
};
new_instr will be 100 (if DEBUG defined), last_plus_1 will be either 101 (if DEBUG defined) or 100 (if DEBUG undefined), and last_instr will be one less than last_plus_1.
Based on the answers provide I came up with a solution that although not perfect do best suit my case.
This implementation can be done in two forms:
Less changes in the future (only change 'last'):
#define push 99
#define last push
#ifdef DEBUG
#define new_instr (1+last)
#define last_instruction new_instr
#else
#define last_instruction last
#endif
OR
Clear Code but repeat 'push' in two places
#define push 99
#ifdef DEBUG
#define new_instr (1+push)
#define last_instruction new_instr
#else
#define last_instruction push
#endif
Thanks for the help.
What does the following statement mean:
#define FAHAD
I am familiar with the statements like:
#define FAHAD 1
But what does the #define statement without a token signify?
Is it that it is similar to a constant definition?
Defining a constant without a value acts as a flag to the preprocessor, and can be used like so:
#define MY_FLAG
#ifdef MY_FLAG
/* If we defined MY_FLAG, we want this to be compiled */
#else
/* We did not define MY_FLAG, we want this to be compiled instead */
#endif
it means that FAHAD is defined, you can later check if it's defined or not with:
#ifdef FAHAD
//do something
#else
//something else
#endif
Or:
#ifndef FAHAD //if not defined
//do something
#endif
A real life example use is to check if a function or a header is available for your platform, usually a build system will define macros to indicate that some functions or headers exist before actually compiling, for example this checks if signal.h is available:
#ifdef HAVE_SIGNAL_H
# include <signal.h>
#endif/*HAVE_SIGNAL_H*/
This checks if some function is available
#ifdef HAVE_SOME_FUNCTION
//use this function
#else
//else use another one
#endif
Any #define results in replacing the original identifier with the replacement tokens. If there are no replacement tokens, the replacement is empty:
#define DEF_A "some stuff"
#define DEF_B 42
#define DEF_C
printf("%s is %d\n", DEF_A, DEF_B DEF_C);
expands to:
printf("%s is %d\n", "some stuff", 42 );
I put a space between 42 and ) to indicate the "nothing" that DEF_C expanded-to, but in terms of the language at least, the output of the preprocessor is merely a stream of tokens. (Actual compilers generally let you see the preprocessor output. Whether there will be any white-space here depends on the actual preprocessor. For GNU cpp, there is one.)
As in the other answers so far, you can use #ifdef to test whether an identifier has been #defined. You can also write:
#if defined(DEF_C)
for instance. These tests are positive (i.e., the identifier is defined) even if the expansion is empty.
#define FAHAD
this will act like a compiler flag, under which some code can be done.
this will instruct the compiler to compile the code present under this compiler option
#ifdef FAHAD
printf();
#else
/* NA */
#endif
#define STR_A abc
#if STR_A == abc //I want to make some check here
do something A
#else
do something B
#endif
Can I check what the STR_A defined?
If you want to check if STR_A is defined to the token abc (which by itself shouldn't be another macro) you can help yourself with some tricks
#define STR_A_TESTER_abc 1
#define CONCAT(A, B) A ## B
#define STR_A_TESTER CONCAT(STR_A_TESTER_, STR_A)
#if STR_A_TESTER
// do whatever
#else
// or other
#endif
That happens quite a lot when trying to distinguish operating systems.
The same can be used for your STR_A.
#if defined(STR_A)
# if (STR_A == "abc")
// do something
# else
// do something else
# endif
#else
#warning "STR_A has not been defined."
#endif