-2

I am attempting to use some code utilising a ternary statement in order to run a specific part, however, when I do this, I get the warning:

expression result unused

And the code in that particular section does not run.

The code in this case is:

i != a ?: printf("|%*s\\\n", i, "");

Why would that be? According to here, this form of the ternary operator, in which there is no alternative for the case, should function, however, it simple skips over it here. Any help is appreciated.

Community
  • 1
  • 1
Bernie
  • 1,489
  • 1
  • 16
  • 27

1 Answers1

2

Your code is equivalent to

(i != a) ? (i != a) : printf(...);

Note that you won't end up using the i != a result, hence the warning. Best to write this as an if statement:

if(i==a) printf(...);
nneonneo
  • 171,345
  • 36
  • 312
  • 383