   C question - am I missing something?
  I have a coworker who's looking to pick up C and his book/tutorial/course whatever asked him to take an array of integers with either a 0 or a 1 in it and flip the value to 1 if 0 and 0 if 1.For the second part of the exercise they asked him to do it without an if statement, for which they gave the following:void flipper (int*numberArray, int arrayLength){int i;for (i = 0; i < arrayLength; i++){numberArray = !numberArray;}}Now, this works on the 3 compilers I tried it on, but as far as I know, 0 is false and true is anything non-zero so there would be no guarantee that this would work. Is there something in the standard that forces the result of !0 to be 1?   
  How aboutnumberArray = 1 - numberArray;;Quote:Original post by linternetNow, this works on the 3 compilers I tried it on, but as far as I know, 0 is false and true is anything non-zero so there would be no guarantee that this would work. Is there something in the standard that forces the result of !0 to be 1?You got the true and non-zero the wrong way around. Anything non-zero is true, but that doesn't mean true is anything non-zero. True is guaranteed to evaluate to integer value 1, and any non-zero value is guaranteed to evaluate to boolean value true.   ;Quote:Original post by linternetI have a coworker who's looking to pick up C and his book/tutorial/course whatever asked him to take an array of integers with either a 0 or a 1 in it and flip the value to 1 if 0 and 0 if 1.For the second part of the exercise they asked him to do it without an if statement, for which they gave the following:*** Source Snippet Removed ***Now, this works on the 3 compilers I tried it on, but as far as I know, 0 is false and true is anything non-zero so there would be no guarantee that this would work. Is there something in the standard that forces the result of !0 to be 1?The result of the ! operator is implicitly converted to type bool, so the result of this operation will always be true or false (ie. 1 or 0).So after firing over the array, all the values that were 0 will now be 1 and everything else will be 0.   ;Quote:Original post by ViLiOThe result of the ! operator is implicitly converted to type boolThere is no bool type in C.As for the OP's question, the inversion of 0 does always equal 1 in C, so it's a safe operation.   ;  Actually in C, the result of ! is an int, despite the fact the C99 added the _Bool type to the language. But yes, ! will result to either 0 or 1, according to the C standard.   ;Quote:Original post by dmatterThere is no bool type in C.My bad [embarrass]   ;  From the C99 standard, 6.5.33.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 expression !E is equivalent to (0==E)."I assume it was the same in C90.   