# C question - am I missing something?

This topic is 3919 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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?

##### Share on other sites

numberArray = 1 - numberArray;

##### Share on other sites
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.

##### Share on other sites
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.

##### Share on other sites
Quote:
 Original post by ViLiOThe result of the ! operator is implicitly converted to type bool

There 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.

##### Share on other sites
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.

##### Share on other sites
Quote:
 Original post by dmatterThere is no bool type in C.

##### Share on other sites
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.

1. 1
2. 2
3. 3
Rutin
14
4. 4
5. 5

• 10
• 11
• 9
• 14
• 10
• ### Forum Statistics

• Total Topics
632652
• Total Posts
3007661
• ### Who's Online (See full list)

There are no registered users currently online

×