-1

I'm trying to implement a decoder for a codec, and while reading the whitepaper I stumbled across this

Variable > 96000 ? 4 : 2;

what does the question mark ? and the colon : between those two numbers do?

I've never seen this before (although I am a noob), and google isn't much help.

user275517
  • 185
  • 1
  • 1
  • 10
  • 8
    Search for "`ternary operator in C`" on Google. – 0xF1 Nov 28 '14 at 04:47
  • 1
    If Variable is larger than 96000, the result is 4. Otherwise 2. – Old Panda Nov 28 '14 at 04:49
  • 3
    Was searching Google too much of a pain in the ass? – Captain Obvlious Nov 28 '14 at 04:50
  • 2
    @CaptainObvlious: how would you find this in Google if you didn't know it was called "ternary operator"? – Eduardo Nov 28 '14 at 04:52
  • @Eduardo [Easy if you think about it](https://www.youtube.com/watch?v=mE3ehB9jpts). – Captain Obvlious Nov 28 '14 at 04:54
  • basic true false if statement with 4 being result if true and 2 being result if false. Very simple don't see how you couldn't find it. Some links - http://www.cprogramming.com/reference/operators/ternary-operator.html , https://en.wikipedia.org/wiki/%3F: , http://msdn.microsoft.com/en-us/library/e4213hs1.aspx, http://www.tutorialspoint.com/cplusplus/cpp_conditional_operator.htm - **first four search results on google** –  Nov 28 '14 at 05:02
  • 2
    Possible duplicate of [What does '?' do in C++?](https://stackoverflow.com/questions/795286/what-does-do-in-c) – GSerg Jan 18 '20 at 21:03

5 Answers5

2

This is ternary operator, this works like if else condition.

Variable > 96000 ? 4 : 2;

In this line if Variable > 96000 is true it will return 4 else it will return 2

A traditional if-else construct in C

if (a > b) {
    result = x;
} else {
    result = y;
}

This can be rewritten as the following statement:

result = a > b ? x : y;
Himanshu
  • 4,327
  • 16
  • 31
  • 39
1

?: is the conditional operator in C.

http://en.wikipedia.org/wiki/%3F%3A

http://www.eskimo.com/~scs/cclass/int/sx4eb.html

ouah
  • 142,963
  • 15
  • 272
  • 331
  • Link only answers are discouraged because if the link becomes old or invalid the response fails to help anyone. – Matt Coubrough Nov 28 '14 at 04:52
  • 2
    It not a link only answer, it gives the name of the operator which should be enough for OP to google it. – ouah Nov 28 '14 at 04:53
0
return ( Variable > 96000 ) ? 4 : 2; 

translates to

if(Variable > 96000){

    return 4;
}else {
    return 2;
}

you are probably missing a return in the front of your statement.

Angel Koh
  • 12,479
  • 7
  • 64
  • 91
0

This is basically an equivalence statement in C , the following example will elaborate its use. In the example below, two numbers are compared and the larger number is returned.

#include <stdio.h>

static int get_larger(int a, int b)
{
return (a > b) ? a : b; // if a is greater than b, return a, else return b
}

int main ()
{
  int a = 100;
  int b = 101;
  printf("Larger Number = %d\n", get_larger(a,b));
  return 0;
}
Khan
  • 464
  • 3
  • 6
  • 18
0

Its ternary operator that is equivalent to If else condition in C/C++.

NOTE:

Its recommended to use parenthesis while using this operator to avoid side-effects of operator precedence problems as mentioned in Unexpected Result, Ternary Operator in Gnu C

Community
  • 1
  • 1
Taimoor
  • 43
  • 5