Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why ternary operator does not support blocks?

Why the ternary operator does not have blocks? In other words, why the following code does not work and reports error for {} braces?

int main()
{
    int i = 1;
    (i==1)?{printf("Hello\n")}:{printf("World\n")};
    return 0;
}

EDIT

Perhaps the question is misunderstood. It was: why blocks are not supported? Why only single expression?

Why this is not allowed to work?

int main()
{
    int i = 1;
    (i==1)?{printf("Hello\n");printf("World\n");}:{printf("Bye\n");printf("World\n");};
    return 0;
}

One reason could be that ternary are often used for conditional assignment on left side and blocks will have no such returns or it will get confusing with multiple statements inside block.

like image 421
Naveen Avatar asked Nov 30 '22 17:11

Naveen


2 Answers

To quote C11 standard, chapter §6.5.15, the syntax of the conditional operator is

conditional-expression:
    logical-OR-expression
    logical-OR-expression ? expression : conditional-expression

Where, the second and third operands are expression, not statements.

Just to elaborate,

One of the following shall hold for the second and third operands:
— both operands have arithmetic type;
— both operands have the same structure or union type;
— both operands have void type;
— both operands are pointers to qualified or unqualified versions of compatible types;
— one operand is a pointer and the other is a null pointer constant; or
— one operand is a pointer to an object type and the other is a pointer to a qualified or unqualified version of void.


Edit:

To answer the question

Why only single expression?

again, quoting the standard,

....the result is the value of the second or third operand (whichever is evaluated), converted to the type described below.

Block of statements, will not give a value. The evaluation of an expression can.

like image 123
Sourav Ghosh Avatar answered Dec 15 '22 13:12

Sourav Ghosh


The ternary operator consists of expressions. There is no such a kind of expression that uses braces.

You can write simply

( i == 1 ) ? printf("Hello\n") : printf("World\n");

It seems that the only case when braces can be present in an expression is the use of a compound literal. For example

struct A { int x; int y; } a = { 1, 2 };

a = a.x < a.y ? ( struct A ){ a.y, a.x } : ( struct A ){ ++a.x, --a.y }; 

As for this statement

(i==1)?{printf("Hello\n");printf("World\n");}:{printf("Bye\n");printf("World\n");};

then it can be rewritten the following way using the comma operator

i == 1 ? ( printf("Hello\n"), printf("World\n") ) : ( printf("Bye\n"), printf("World\n") );

Or even like

i == 1 ? printf("Hello\n"), printf("World\n") : ( printf("Bye\n"), printf("World\n") );

Shortly answering your question if you need a code block then use the if-else statement instead of the ternary operator. Though the if-else statement may not be used in expressions. On the other hand it is desirable for readability of the code that expressions would not be too compound.

As any operator the ternary operator is used in expressions and returns some evaluated value. For example as an expression it can be used as initializer or in assignments.

like image 32
Vlad from Moscow Avatar answered Dec 15 '22 15:12

Vlad from Moscow