Ternary operator in C?

nested ternary operator in c
ternary operator in c tutorialspoint
ternary operator in c#
ternary operator in c for 3 variables
ternary operator in c for if
ternary operator java
conditional operator in c
ternary operator js

Why is the output displayed as 3? What is the concept behind this?

#include <stdio.h>

int main()
{
    int a = 5, b = 4, c;
    c = a > b ? 1, 2, 3 : 2, 5, 6;
    printf("%d\n", c);
    return 0;
}

The line of code containing the ternary operator is intricate and intriguing. It also has subtle but crucial asymmetries in the way it is interpreted that are far from obvious when it is written as shown in the question. Let's consider a minor variant of the original code as follows, which exercises both the 'true' and 'false' parts of the ternary operator (in two separate statements — though I could have made a function and passed arguments to that instead):

#include <stdio.h>

int main(void)
{
    int a = 5, b = 4, c;
    c = a > b ? 1, 2, 3 : 2, 5, 6;
    printf("a = %d, b = %d, c = %d\n", a, b, c);
    a = b - 1;
    c = a > b ? 1, 2, 3 : 2, 5, 6;
    printf("a = %d, b = %d, c = %d\n", a, b, c);
    return 0;
}

The output from this program is:

a = 5, b = 4, c = 3
a = 3, b = 4, c = 2

What's going on here?

Remember, the comma operator has even lower precedence than the assignment operator, so that you can, if you wish, write code such as:

if (a > b)
    c = 2, d = 3, e = 6;

and there are three assignments in the body of the if statement. That would not be regarded as good code for general use, but the technique can be useful in a macro — very occasionally it can be useful in a macro.

Let's add some parentheses — the correct set of parentheses as the compiler interprets the code:

 c =  a > b  ?  1, 2, 3  : 2 , 5, 6;
(c = (a > b) ? (1, 2, 3) : 2), 5, 6;

The condition a > b controls whether 1, 2, 3 or 2 is evaluated, and the result is assigned to c. The result of 1, 2, 3 is 3, so if a > b, the value assigned is 3 (as in the question), and if a <= b, the value assigned is 2. The 5 and the 6 are evaluated for side-effects — but there are no side-effects so they are effectively discarded.

The : of the ternary operator has a grouping effect on the code between the ? and the : which does not occur after the :.

Note that to get this code to compile, I had to discard my default compiler options. With the code in the question and my normal, default compiler options, I get (source code in top67.c):

$ gcc -O3 -g -std=c11 -Wall -Wextra -Werror -Wmissing-prototypes -Wstrict-prototypes \
>     top67.c -o top67 
top67.c:3:5: error: function declaration isn’t a prototype [-Werror=strict-prototypes]
 int main()
     ^~~~
top67.c: In function ‘main’:
top67.c:6:18: error: left-hand operand of comma expression has no effect [-Werror=unused-value]
     c = a > b ? 1, 2, 3 : 2, 5, 6;
                  ^
top67.c:6:21: error: left-hand operand of comma expression has no effect [-Werror=unused-value]
     c = a > b ? 1, 2, 3 : 2, 5, 6;
                     ^
top67.c:6:28: error: right-hand operand of comma expression has no effect [-Werror=unused-value]
     c = a > b ? 1, 2, 3 : 2, 5, 6;
                            ^
top67.c:6:31: error: right-hand operand of comma expression has no effect [-Werror=unused-value]
     c = a > b ? 1, 2, 3 : 2, 5, 6;
                               ^
cc1: all warnings being treated as errors
$

I'd get more errors from the code shown in this answer. You should be compiling with similar options to avoid problems in your own code.

?:, Since the Conditional Operator '?:' takes three operands to work, hence they are also called ternary operators. Working: Here, Expression1 is the condition to be  In C++, ternary operator allows executing different code depending on the value of a condition, and the result of the expression is the result of the executed code. The ternary operator uses 3 operands. It evaluates a condition and after that chooses one of its two branches to execute, depending upon the result of condition.

This code:

c = a > b ? 1, 2, 3 : 2, 5, 6;

is understood by the compiler as:

(c = a > b ? (1, 2, 3) : 2),
5,
6;

In the C and C++ programming languages, the comma operator (represented by the token ,) is a binary operator that evaluates its first operand and discards the result, and then evaluates the second operand and returns this value (and type). If there are more than two operands, the last expression will be returned.

At this condition (c = a > b ? (1, 2, 3) : 2)

a > b will return true

so it will evaluate first operand 1 and discards the result then evaluate the second operand 2 and discard the result then evaluate the third operand 3 and return its value which is 3

and then 5, 6; will be discarded after evaluating them because they don't produce any side effects.

at the end the output of program will be 3.

but if we consider a < b at condition (c = a < b ? (1, 2, 3) : 2)

in this case it will evaluate else condition and return its value which is 2

also 5, 6; will be discarded after evaluating them because they don't produce any side effects.

at the end the output of program will be 2.

Ternary Operator in C Explained, It is commonly referred to as the conditional operator, inline if (iif), or ternary if. An expression a ? b : c evaluates to b if the value of a is true,  Ternary Operator in C. If any operator is used on three operands or variable is known as Ternary Operator. It can be represented with ? :. It is also called as conditional operator. The ternary operator is an operator that takes three arguments.

if (a>b)

value of c = expect integer expression before : token

else

value of c = expect integer expression after : token

if c is integer type and we are using ternary operator ? then compiler expects integer data type before and after : token

if we are using unsigned integer data type, it will convert it to signed integer data type

Conditional or Ternary Operator (?:) in C/C++, Try following example to understand ternary operator. You can put the following code into a test.c file and then compile it and then run it . Ternary operator is an operator which can be used in place of an if else condition when both if and else part has only one line inside them. Lets look at the syntax of ternary operator in C language and understand ternary operators with example. Syntax of Ternary Operators in C. Here is the syntax of ternary operator along with its if else

Ternary Operator - ?:, The ternary operator is used to execute code based on the result of a binary condition. It takes in a binary condition as input, which makes it similar to an 'if-​else'  Then, the ternary operator is used to check if number is even or not. Since, 2 is even, the expression (number % 2 == 0) returns true. We can also use ternary operator to return numbers, strings and characters. Instead of storing the return value in variable isEven, we can directly print the value returned by ternary operator as,

ternary operator examples:, b : c) ? d : e . Points to Remember : Ternary operator: boolean expression ? first statement : second statement  Introduction The conditional operator is an operator used in C and C++ (as well as other languages, such as C#). The ?: operator returns one of two values depending on the result of an expression. Syntax (expression 1) ? expression 2 : expression 3 If expression 1 evaluates to true,

Ternary Operator in C - SyntaxDB, The ternary operator is a syntactic and readability convenience, not a performance shortcut. People are split on the merits of it for conditionals of varying  This article explains ternary operator in C#. C# includes a special type of decision making operator '?:' called the ternary operator.

Comments
  • and why would you write like a code like that? can you spell the logic in simple English? if no, then dont write the code like that.
  • You are putting yourself at risk of downvotes by people who fight for the recognition of C and C++ as separate languages. (I did not downvote.)
  • Although this is only another writing for the ifquery.
  • This question gives the strong impression of being a homework assignment. Most users here do not take kindly to being asked to do other peoples (home)work. Please state whether this is homework and whether you would appreciate help according to the compromise described here: meta.stackoverflow.com/questions/334822/…
  • @Alan: Do you want to check what happens if you invert the condition? When does 6 get assigned to c?
  • You may what to use code blocks or inline code format (back ticks ` ` around your code) to make the answer easier to read