#define TRUE! FALSE vs #define TRUE 1

Putting aside the fact that with c99 stdbool.hexisted, when defining macros for processing boolean types, is Cthere any difference between the following?

#define FALSE 0

#define TRUE 1       // Option 1 
#define TRUE !FALSE  // Option 2

From the live example here , it doesn't seem to matter. Is there any technical benefit to any option? (Apart from the fact that the second example will work better with C ++ objects bool.)

+4
source share
6 answers

ISO C and C99 both define !this.

The result of the logical negation operator! 0, if the value of its operand is not equal to 0, 1, if the value of its operand is compared to 0. The result is of type int. Expression! E is equivalent (0 == E).

So !0 1. C , . , , !0 1 .


, ...

#define TRUE  (1==1)
#define FALSE (!TRUE)

. , 0 "" " ".

, C . , Code Complete 369. 1993 , , C ISO, stdbool.h . "Code Complete" Polyglot, . , shell Lisp, -.

+8

2, ! 0 C 1.

TRUE , , " ".

+3

.

#define TRUE 1 #define TRUE !FALSE , 1 - , .

!FALSE (!FALSE), , ++ -- [] . ->, FALSE.

+2
#define FALSE 0

#define TRUE 1       // Option 1 
#define TRUE !FALSE  // Option 2

. 1 !0 int , 1 ( !).

, . , . unparenthesized . .

! , . , , - . , :

int arr[] = { 10, 20 };

1 :

TRUE[arr] == 20

2 :

TRUE[arr] == 0

, , , (. , [] , !.

:

  • , , - , .

  • . C, 0 - , 1 - . ( "true", "" 0 1.) ! TRUE FALSE ( ) .

<stdbool.h>, . ( pre-C99), :

typedef enum { false, true } bool;

, C99 _Bool/bool ( bool 0 1), .

+2

C TRUE (! FALSE), , (0) FALSE, FALSE (0), TRUE. , , TRUE. NULL . ('\ 0'). , , . :

while ( *d++ = *s++ ); 

. . .

, TRUE, , TRUE FALSE. , . .

0

.

  • !0 1, !FALSE 1

#define TRUE !FALSE , .

#define TRUE !FALSE , , TRUE , 0.

  • 1 TRUE, , 2, 3, 255... ( !=0) TRUE

To prevent this misunderstanding , many organizations no longer require use #define TRUE !FALSE, or the comparison with TRUEshould be changed to !FALSE:

// Should not     
if (var_bool == TRUE) {
...
}

//Should    
if (var_bool != FALSE) {
...
}
-2
source

Source: https://habr.com/ru/post/1629971/


All Articles