Jump to content
  • Advertisement
Sign in to follow this  
linternet

C question - am I missing something?

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

If you intended to correct an error in the post then please contact us.

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


Link to post
Share on other sites
Advertisement
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
Quote:
Original post by ViLiO
The 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 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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!