What does “gcd = x < y ? x : y” mean? [duplicate]

  • A+
Category:Languages

This question already has an answer here:

What does gcd = x < y ? x : y mean?

Does it mean that gcd is equal to the smaller one between x and y?

 


Yes it does. The ?: operator, often known as the ternary operator because it is the only operator in C with three operands, evaluates its first operand and returns the second operand if it is true, or the third operand if it is false.

An example. Let's say you want to print the value of a boolean variable for debugging purposes:

printf("The value of a is: %s/n", a ? "true" : "false"); 

which is almost1 equivalent to

char *s; if (a) {     s = "true"; } else  {     s = "false"; } printf("The value of a is: %s/n", s); 


1a new variable s has been defined.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: