Sign in to follow this  
linternet

C question - am I missing something?

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[i] = !numberArray[i];
	}
}


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 this post


Link to post
Share on other sites
Quote:
Original post by linternet
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?

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 this post


Link to post
Share on other sites
Quote:
Original post by linternet
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:

*** 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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this