Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usage of NOT operator in #define directives in C programming

I have defined macros as below.

#define FALSE   0
#define TRUE    (!FALSE)

What will be the data-type of TRUE and FALSE? What literal value does TRUE take after preprocessing? Is it compiler dependent? Why?

like image 486
vinoth kumar Avatar asked Feb 09 '23 07:02

vinoth kumar


1 Answers

#define preprocessor directive (macros) are meant to do textual replacement. It will replace all occurrence of FALSE to 0 and TRUE to !0 that essentially gets evaluated to 1. So, the resultant data type will be same as 0 and 1. i.e., integer.

Regarding the usage of ! operator, it always produces a result of type int.

Quoting the C11 standard, chapter §6.5.3.3 (emphasis mine)

The result of the logical negation operator ! is 0 if the value of its operand compares unequal to 0, 1 if the value of its operand compares equal to 0. The result has type int. [...]

like image 109
Sourav Ghosh Avatar answered Feb 11 '23 21:02

Sourav Ghosh