0

I want to use TRUE and FALSE macros in my custom header file which is intended to be used by different .c files as some common macro definitions file.

The problem I encountered is as follows: Both 3dparty and I define the same macros but those values can differ (TRUE or FALSE can be a pretty simple situation as that is more obvious which values go where).

I want to emphasize that I can`t include that 3party header in my custom code, as that library is not the common thing used in each project. What is more, it seems strange? (or no - but it works though:) ). The solution I see could be also doing some prefixes like CUSTOM_TRUE - but it looks ugly.

Or I shouldn`t bother about it at all and just make it work in any way I choose?

Some case modelling:

main.c

#include "3dparty.h"
#include "b.h"

/* TRUE defined from "custom" header and == 2
 - in this case we are good */

int res = cus_is_digit("1");
if (res == TRUE)
{
    ...
}

/* 3dparty is returning TRUE as 1 but we are checking for 2 */
int res2 = is_something(...);
if (res2 == TRUE)
{

} 

...

b.h

#undef TRUE
#define TRUE 2
#endif

OR

#ifndef
#define TRUE 2
#endif

/* This file uses TRUE macro defined with value 2. */
/*Sorry for definition in header */  
int cus_is_digit(const char *sdigit)
{
    /* some logic here */
    return TRUE; /* return 2; */
}
...

3dparty.h

#define TRUE 1

int is_something(...);
...

3dparty.c

 #include 3dparty.h

 int is_something(...)
 {
     ...
     return TRUE; /* return 1; */
 }
    
 ...
Svutko
  • 11
  • 3
  • How about... not use macros? https://stackoverflow.com/questions/1921539/using-boolean-values-in-c – littleadv Jan 08 '22 at 23:37
  • 3
    *`#define TRUE 2`* That makes me want poke out my eyes with a flaming stick and then pour bleach in the bloody holes left behind in an attempt to unsee that. Never do that unless your goal is to deliberately confuse everyone reading your code in the future. – Andrew Henle Jan 08 '22 at 23:50
  • @Andrew Henle, I feel you, sorry for that - I did it just to give an example, haha. – Svutko Jan 09 '22 at 08:58

1 Answers1

1
  1. There is no one "proper" way of doing it.
  2. If you have the same macro names and they mean something completely different then the only way is to use a different more specific names.

Example:

Another header file defines

#define LINELENGTH   64

And your code requires different size - simple add some prefix in your header file. if your module is called usart then

#define USART_LINELENGTH 128
0___________
  • 60,014
  • 4
  • 34
  • 74