3

Is it okay (defined behavior) to add up the result of logical operations (as they should just be 0 or 1)?

Can I do something like this if I want to count the numbers bigger than zero?(or is there a better way?)

int a[3] = {1,-5,3};
int result  = 0;
for( int i = 0 ; i<3; i++)
{
    result += a[i]>0;
}
Kami Kaze
  • 2,069
  • 15
  • 27

1 Answers1

5

Quoting C11, chapter §6.5.8, (emphasis mine)

Each of the operators < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to) shall yield 1 if the specified relation is true and 0 if it is false.107) The result has type int.

Then you're performing a compound assignment, where it is required that

For the operators += and -= only, either the left operand shall be an atomic, qualified, or unqualified pointer to a complete object type, and the right shall have integer type; or the left operand shall have atomic, qualified, or unqualified arithmetic type, and the right shall have arithmetic type.

and your expression satisfies the constraint.

So yes, it is defined behaviour.


That said, from a syntax perspective, you're only safe because of the default operator precedence matches your expectation. There's no problem as such, but being explicit (and thus, sure) never hurts.

You can rewrite the expression as

 result += ( a[i] > 0 );
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • Do you know where to find relevant parts of the standard on the top of your head? or how can you be so fast?^^ – Kami Kaze Mar 22 '17 at 13:41
  • @KamiKaze All operator groups are listed in chapter 6.5 so it is just a matter of opening the pdf and click on the relevant group :) – Lundin Mar 22 '17 at 13:43
  • @Lundin I think I will ditch the next book I want to read and read the full standard instead. That way I know where to find what – Kami Kaze Mar 22 '17 at 13:45
  • 1
    @StoryTeller: Fixed the double negative :-). – sleske Mar 22 '17 at 13:45
  • @sleske With all due respect, that double negative was **not** a typo. Standard mentions the term "undefined behaviour", but it never mentions "defined behaviour", only implies. – Sourav Ghosh Mar 22 '17 at 13:46
  • 1
    @KamiKaze - The standard is a really bad resource to *learn* the language if you wish to do it fast. You'd be better served to finish the book first, and then graduate to the standard. – StoryTeller - Unslander Monica Mar 22 '17 at 13:47
  • @StoryTeller Do I sound like that I need to learn the language? (honest question) On what parts would you deduct that? Because I am not reading a book about programming at the moment I thought my skills in C were a good basis at least. (Or did you just assumed it because of my book statement, As I said this is an honest question If I have deficits I have to remove I am glad to learn about those) – Kami Kaze Mar 22 '17 at 13:54
  • @KamiKaze - Meant no offence. I (perhaps erroneously) deduced from you yourself saying *"I think I will ditch the next book I want to read and read the full standard instead"*. – StoryTeller - Unslander Monica Mar 22 '17 at 13:55
  • @SouravGhosh: Sorry, no disrespect intended. It just that, as pointed out by StoryTeller, "not undefined" sounds weird in English. It's true the standard never mentions "defined behavior", but that's probably just because any behavior described in the standard is by definition defined. Also, many texts on C use the expression "behavior is defined" or similar. Anyway, feel free to change it back if you feel it is clearer that way. – sleske Mar 22 '17 at 14:41
  • @sleske Nothing to be sorry for, was just trying to point out that the other version was also "conforming". I'll leave it to other language-lawyers to have a look at it. :) – Sourav Ghosh Mar 22 '17 at 14:43