Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is !0 in C?

Tags:

c

boolean

I know that in C, for if statements and comparisons FALSE = 0 and anything else equals true.

Hence,

int j = 40
int k = !j

k == 0 // this is true

My question handles the opposite. What does !0 become? 1?

int l = 0
int m = !l

m == ? // what is m?
like image 532
Raven Dreamer Avatar asked Sep 07 '10 18:09

Raven Dreamer


People also ask

What is the use of '\ 0 in C?

\0 is zero character. In C it is mostly used to indicate the termination of a character string. Of course it is a regular character and may be used as such but this is rarely the case. The simpler versions of the built-in string manipulation functions in C require that your string is null-terminated(or ends with \0 ).

Is 0 and null the same in C?

Null is a built-in constant that has a value of zero. It is the same as the character 0 used to terminate strings in C.

What is meant by 0 in programming?

Zero is the lowest unsigned integer value, one of the most fundamental types in programming and hardware design. In computer science, zero is thus often used as the base case for many kinds of numerical recursion. Proofs and other sorts of mathematical reasoning in computer science often begin with zero.

Is 0 the same as null?

The answer to that is rather simple: a NULL means that there is no value, we're looking at a blank/empty cell, and 0 means the value itself is 0. Considering there is a difference between NULL and 0, the way Tableau treats these two values therefore is different as well.


2 Answers

Boolean/logical operators in C are required to yield either 0 or 1.

From section 6.5.3.3/5 of the ISO C99 standard:

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.

In fact, !!x is a common idiom for forcing a value to be either 0 or 1 (I personally prefer x != 0, though).

Also see Q9.2 from the comp.lang.c FAQ.

like image 162
jamesdlin Avatar answered Oct 22 '22 20:10

jamesdlin


§6.5.3.3/5: "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."

The other logical operators (e.g., &&, ||) always produce either 0 or 1 as well.

like image 26
Jerry Coffin Avatar answered Oct 22 '22 21:10

Jerry Coffin