最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

In C, Is parenthesizing the first operand of the conditional operator ever actually required? - Stack Overflow

programmeradmin10浏览0评论

I realize that many programmers parenthesize the first operand of the conditional operator just to make the conditional expression easier to read, but is there any situation where parenthesizing it or not parenthesizing it would make a functional difference? I haven't been able to come up with any.

expr1 ? expr2 : expr3    vs.   (expr1) ? expr2 : expr3

I realize that many programmers parenthesize the first operand of the conditional operator just to make the conditional expression easier to read, but is there any situation where parenthesizing it or not parenthesizing it would make a functional difference? I haven't been able to come up with any.

expr1 ? expr2 : expr3    vs.   (expr1) ? expr2 : expr3
Share Improve this question asked Dec 5, 2024 at 12:03 BenevolentDeityBenevolentDeity 7831 gold badge5 silver badges19 bronze badges 1
  • The rule is parenthesize conditional expressions because ... well because (not really but ya, the space in this comment is too small). So simply follow the rule with no exceptions!! I also do bool isfortytwo(int a) { return (a == 42); } – pmg Commented Dec 5, 2024 at 12:18
Add a comment  | 

4 Answers 4

Reset to default 8
#include <stdio.h>

int main () {
    int a;
    int b;
    int c;

    a = 1;
    b = 0;
    c = a = b ? 42 : 4242;
    printf("a = %-8d b = %-8d c = %-8d\n", a, b, c);
    
    
    a = 1;
    b = 0;
    c = (a = b) ? 42 : 4242;
    printf("a = %-8d b = %-8d c = %-8d\n", a, b, c);
    
    return 0;
}

Output:

a = 4242     b = 0        c = 4242    
a = 0        b = 0        c = 4242    

But I guess nobody would write code like that (at least I hope so).

Edit

To show the difference without and with parentheses the code below is simpler and equivalent:

    // c = a = b ? 42 : 4242;
    a = b ? 42 : 4242;
    c = a;


    // c = (a = b) ? 42 : 4242;
    a = b;
    c = a ? 42 : 4242;

Below is another example. In this case both a and c end up having different values.

#include <stdio.h>

int main () {
    int a;
    int b;
    int c;

    a = 1;
    b = 1;
    c = a -= b ? 42 : 4242;
    printf("a = %-8d b = %-8d c = %-8d\n", a, b, c);
    
    
    a = 1;
    b = 1;
    c = (a -= b) ? 42 : 4242;
    printf("a = %-8d b = %-8d c = %-8d\n", a, b, c);
    
    return 0;
}

Output:

a = -41      b = 1        c = -41     
a = 0        b = 1        c = 4242 

Looking at https://en.cppreference.com/w/c/language/operator_precedence only assignments and comma are below ternary expression. The only case would be assignment:

int a;
a = 1 ? 2 : 3;

vs

(a = 1) ? 2 : 3;

And similar for all the other >>= <<= etc.

Because the comma operator always gives the result of the right-most expression, adding braces doesn't change anything. But with comma with assignment then:

a = 1, 0 ? 2 : 3;

results in the same as:

(a = 1, 0) ? 2 : 3;

but is different from:

a = (1, 0) ? 2 : 3

It is opinion based.

When expr1 involves multiple operations, such as relational (> or <), logical (&& or ||), or arithmetic (+, -), parentheses ensure proper grouping and intended behaviour.

Without parentheses:

  • Ambiguity in evaluation order.
  • Risk of logic errors.
  • Potential invalid or unexpected expressions due to operator precedence.

IMO: Always use parentheses for clarity when expr1 is a complex expression.

Consider misleading style: a=b + c? .... This is easily read incorrectly as "assign b to a then add the result with c and check condition. While it does in fact add b and c together first, then check the result versus non-zero/zero, then assigns something to a.

It is generally considered good practice to use extra parenthesis around sub-expressions, where the operator precedence isn't necessarily obvious. Nor do we generally expect C programmers to know all the dirty details of operator precedence.

Another classic example of a bug would be if(a == b&mask) which even Dennis Ritchie admitted being a design mistake in C. Correct code as intended should be if(a == (b & mask)).

Is there any situation where parenthesizing it or not parenthesizing it would make a functional difference?

int a;
a = 0, 1+1 ? 1 : 0;
printf("%d\n", a);

versus

a = (0, 1+1) ? 1 : 0;

Basically anything involving assignment or comma operators which have lower precedence than ?:.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论