C question - am I missing something?

Started by
6 comments, last by the_edd 16 years, 3 months ago
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?
Advertisement
How about

numberArray = 1 - numberArray;

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.
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.
Richard 'ViLiO' Thomasv.net | Twitter | YouTube
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.
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 dmatter
There is no bool type in C.
My bad [embarrass]
Richard 'ViLiO' Thomasv.net | Twitter | YouTube
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.

This topic is closed to new replies.

Advertisement