-4

Is it possible in C to write something like? and if so how is it done?

if ( x != 1, 2, 7, 8, 90)
  • 1
    Rather than guessing, how about getting a good tutorial or reference ?to learn about C syntax? – John Bollinger Sep 02 '17 at 02:10
  • why downvote? Adam set 'niche' question about C formalism. Disputed code has paradoxical behaviour (how big paradox feeling depends: what is expected), but is legal. No requremets here "I want compare all and give result 'at least one is true / false" – Jacek Cz Sep 02 '17 at 02:11
  • Didn't downvote, but I'd assume since C syntax is trivially searchable, there's little reason to ask here. – Dave Newton Sep 02 '17 at 02:20
  • In any case, if your concern is length, then just use a function that takes an array and checks against each element. – Dave Newton Sep 02 '17 at 02:22

3 Answers3

3

You can't compare multiple values that way. You need to check each individually combined with the proper logical operator:

if ((x != 1) && (x != 2) && (x != 7) && (x != 8) && (x != 90)

What you wrote is something entirely different. This:

if ( x != 1, 2, 7, 8, 90)

Is a single comparison operator followed by several instances of the comma operator. The comma operator evaluates the left operand, discards the value, then evaluates the right operand and takes its value.

So that expression first evaluates x != 1 which will result in either 0 or 1. That value is discarded and the value 2 is evaluated. That value is in turn discarded and the next value (7) is evaluated. Then 8 is evaluated and discarded, then 90 is evaluated which becomes the value of the entire expression.

dbush
  • 205,898
  • 23
  • 218
  • 273
0

No you can't do that. You need to write it like this :

if (x != 1 && x != 2 && x!= 3)

The comma operator has another effects, I let you looking for it :)

Gam
  • 684
  • 1
  • 8
  • 17
-2

My answer is paradoxical, but strictly correct.

Is it possible in C to write something like? and if so how is it done?

a) Yes, it is possible.

b) How is done? Here is comma operator, yes, C has comma operator. Final value is last element, 90 here

c) The question author didn't ask about comparison

So this if has sense if(90). Interesting is that sub-expression x!=1 is evaluated, but result is lost, and has no effect on final

if ( x != 1, 2, 7, 8, 90)
{
  here, true
}
else
{
  not here, false
}

Adan didn't ask how to compare, but the question posed above.

Appendix: fully working code from MSVC. Everything I say, anyone can debug.

#include "stdafx.h"


int main()
{
    int x = 2;
    int k = (x != 1, 2, 7, 8, 90);
    if (x != 1, 2, 7, 8, 90)
    {
        int k1 = 1;
    }
    else
    {
        int k2 = 2;
    }
    return 0;
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Jacek Cz
  • 1,872
  • 1
  • 15
  • 22
  • 2
    Didn't downvote, but sometimes you have to use some common sense in interpreting what a question is really asking. IMO the OP's intent is clear, and a comparison against multiple values is that intent. Could be wrong, but I doubt it. – Dave Newton Sep 02 '17 at 02:24
  • @DaveNewton Your words are logical. But programming and SO is based on concrete defined/standardised behaviour, not imagination / intent "post above: imagination how should work is worst enemy". If someone has intent, lets write in free text "i want comparing". Many professional answers to stupid questions are downvoted here because "are not direct related to exact question". My answer is small psychological test - provocation - strict used rules (like in army) "how to answer" . I'm not so young developer, is few points in social media is no problem. Cheers :) – Jacek Cz Sep 02 '17 at 08:09