-
Opperators
As Iv'e been journeying through verious texts iv'e come accross simmalar lines of code such as this one a few times:
Code:
#define MIN(X,Y) ( ((X) < (Y)) ? (X) : (Y) )
Now, I know what this does... to a degree the macro part and format i understand but it's the opperators '?' and ':' I don't have a clue what they mean and none of the texts i'm reading appear to explain it.
-
K&R calls it the conditional operator, see A7.16 of K&R-2.
a = MIN(x,y)
is
if ( x < y ) a = x ; else a = y;
but in a form you can use in an expression.
-
Oh ok,
so basicly the '?' is the opperator equivilant of the 'IF' command and ':' is the same as Else, just a little differently written
-
Yes, but you have to must use them both together and the syntax isn't exactly the same (e.g. the condition goes after the if but before the ?).
-
-
The C++ Standard Library has min() from <algorithm> which does the same thing more safely, without the macro.
http://www.cplusplus.com/reference/algorithm/min.html