Why is it wrong when I try to assign a value to the variable within a conditional operator?

Asked

Viewed 211 times

4

I have the following code that should receive 4 numbers and tell which one is the largest.

#include <stdio.h>

int max_of_four(int x, int z, int y, int w);

int main() {
    int a, b, c, d;
    scanf("%d %d %d %d", &a, &b, &c, &d);
    int ans = max_of_four(a, b, c, d);
    printf("%d", ans);

    return 0;
}


int max_of_four(int x, int z, int y, int w){
    int max1, max2;

    x > z ? max1 = x : max1 == z;
    y > w ? max2 = y : max2 == w;


    return max1 > max2 ? max1: max2;
}

My doubt is in the function max_of_four. I compare two numbers twice and the largest should be assigned to their respective variable. In the else (... : max1 == z; / ... : max2 == w;) instead of == should be =, but if I put the = the code gives the error:

lvalue required as left operand of assignment

On the other hand, if I leave the == the code does not run right, not printing the highest value if it is in second or fourth position.

I know I could do the following:

 int max1 = x > z ? x : z;
 int max2 = y > w ? y : w;

But I’d like to know if my code is incorrigible or if I’m missing something.

  • 1

    There is not much point in this == because the result will be true or false and not an assignment. The correct is what you put in the end.

  • Function was created after. I’ve had this problem. Always leave main on the last lines

  • 2

    Try: (x > z) ? (max1 = x) (max1 = z).

  • 2

    @Maurydeveloper The function is at the end, but I declared it at the top before the main function. The tip to put in parentheses the terms worked and Luiz Augusto explained well what happens. Thank you to all!

2 answers

4

You can do what you want, only the syntax was a little wrong, see that it can be even simpler:

#include <stdio.h>

int max_of_four(int x, int z, int y, int w) {
    int max1 = x > z ? x : z;
    int max2 = y > w ? y : w;
    return max1 > max2 ? max1 : max2;
}

int main() {
    int a, b, c, d;
    scanf("%d %d %d %d", &a, &b, &c, &d);
    int ans = max_of_four(a, b, c, d);
    printf("%d", ans);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Ah, but you want to do it another way, so yes, your code is incorrigible. Your taste cannot override the most correct.

You are mixing statement with expression. The conditional operator exists to simplify when you have expressions. You are wanting to transform a code from statement in expression and this is wrong even if it works. You are making the code more unreadable, so don’t do it, simple like this.

An assignment generates a side effect and this type of code is the most cause bugs, so it should be very visible, it should be at the beginning of the line to be obvious. You’re using an operator to replace a language construct and "shorten," that’s not cool. Why not use the operator the right way so it shortens even more?

  • Got it. So should I just use a ternary operator when I want to assign a value to a variable under one condition? I thought it was a shorter form of writing, I did not know it had certain syntactic conditions for the Op. Ternary. Thank you very much for the theoretical correction!

2


I checked your code with ternary operator and do not compile, but using the selection structure if/else the same compilation.

#include <stdio.h>

int max_of_four(int x, int z, int y, int w);




int max_of_four(int x, int z, int y, int w){
    int max1, max2;

    if (x > z) 
    max1 = x;
    else
    max1 = z;

    if (y > w)
     max2 = y;
     else
      max2 = w;


    return (max1 > max2 ? max1 : max2);
}
int main() {
    int a, b, c, d;
    scanf("%d %d %d %d", &a, &b, &c, &d);
    int ans = max_of_four(a, b, c, d);
    printf("%d", ans);

    return 0;
}

Putting the search on google: lvalue required as left operand of assignment Ternary Operator I find the following questions:

  1. "lvalue required as left operand of assignment" error - Ternary Operator
  2. Error: lvalue required in this simple C code? (Ternary with assignment?)

Translating, via google, reply:

In the C language, the grammar and semantics of the conditional operator are different from C++. Your code would be compiled in C++, since in C++ the expression

 <condition> ? a = b : c = d

would be analyzed as

 <condition> ? (a = b) : (c = d)

But in C, the same expression is analyzed as

 (<condition> ? (a = b) : c) = d

which is a completely different story. The result of ?: in C is never a lvalue, and that is why the last analysis is not compiled.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.