1

I must have a syntax problem, but I cannot understand what this means - I googled, but I didn't understand the cause of this problem. Please tell me what is wrong in my function syntax

This is the line with the error:

else sequence= (Bnum < Anum) ? sequence=Bnum : sequence=Anum;

This is the function

int reduction(int Knum)
{
    int sequence=8, Anum=0, Bnum=0;

    printf("enter a sequence,to stop print (-1)\n");
    while(sequence!=-1)
    {
        Anum = sequence;
        scanf("%d",&sequence);
        Bnum = sequence;
        if (Anum+Bnum<8)
            return 1;
        else
            sequence= (Bnum < Anum) ? sequence=Bnum : sequence=Anum;
    }
    return 0;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sveta26
  • 541
  • 2
  • 8
  • 19

5 Answers5

7

The following

sequence= (Bnum < Anum) ? sequence=Bnum : sequence=Anum ;

should be written as:

sequence= (Bnum < Anum) ? Bnum : Anum ;

Observe an interesing difference between C and C++

I'm sure you're compiling your code as C program, rather than C++ program, because in C++ what you've written would not result in compilation error, though it would be an error in C. It is one instance where C and C++ differs!

As for the explanation of the error, read my post here:

Community
  • 1
  • 1
Nawaz
  • 353,942
  • 115
  • 666
  • 851
5
else sequence = (Bnum < Anum) ? Bnum : Anum;

You don't need the extra assignment in the ?: Operator.

Sylence
  • 2,975
  • 2
  • 24
  • 31
4

You need to read about the precedence of the ?: and = operators. You are hoping that

sequence = (Bnum < Anum) ? sequence=Bnum : sequence=Anum;

(even though it's wrong for the too-many-simultaneous-assignments-to-sequence reasons) is parsed as

sequence = (Bnum < Anum) ? (sequence=Bnum) : (sequence=Anum);

but the rules of the language (in C) are that if you want that parsing, you have to use parentheses yourself. Without explicit parentheses, the precedence of the operators means the expression is equivalent to

sequence = ((Bnum < Anum) ? (sequence=Bnum) : sequence) = Anum;

which is meaningless in C, hence the syntax error.

But even more, you need to think about what value you want to store into sequence. Why does sequence = ... appear so many times in this statement? See also my answer to this question.

Community
  • 1
  • 1
John Marshall
  • 6,815
  • 1
  • 28
  • 38
  • 2
    I don't think this is true for *both* C and C++. –  Dec 11 '11 at 10:41
  • 2
    It doesn't parse in C. `sequence=Anum` is not a valid _conditional-expression_ as the last operand to `?:` and `(Bnum < Anum) ? sequence=Bnum : sequence` is not a valid _unary-expression_ as the first operand to `=`. – CB Bailey Dec 11 '11 at 11:05
  • @pst: Interesting, so C++ fixed this to get the expected parse, and the parenthesis-less expression parses differently in C and C++. Another example of "don't do that then" for programmers who write in both languages :-). (At least, I assume that is what your rather content-free comment is getting at...) – John Marshall Dec 11 '11 at 12:31
  • In C++ the _assignment-expression_ and _condition-expression_ are much more liberal. The lhs of an _assignment-expression_ can be a full _logical-OR-expression_ (not just a _unary-expression_) and the last operand of `?:` can be an `_assignment-expression_`, not just another `_conditional-expression_`. The first change, I assume, is because you are more likely to get an lvalue out of a compound expression in C++; the second - I speculate - is because it doesn't break anything that would work in C and you are more likely to get the expected results in (e.g.) `a ? z = 0 : z = 1`. – CB Bailey Dec 11 '11 at 12:39
  • @Charles: Well, I know that and you know that, but do you think Sveta is in a position to need to know that in this stage of her C programming career? :-) – John Marshall Dec 11 '11 at 12:49
3

You should assign to sequence just once:

else {
    sequence = (Bnum < Anum) ? Bnum : Anum ;
}

In C++ you could also use min here.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • I don't believe he can use `std::min`. I'm sure it is a C program, otherwise it wouldn't give compilation error. See my answer for detail. – Nawaz Dec 11 '11 at 10:15
  • He? I doubt that someone called "Sveta" is a "he". Besides, the question is tagged with both C and C++ so it seems relevant to answer the question with regards to both. – Mark Byers Dec 11 '11 at 11:40
1

In C#, the syntax would be sequence = (BNum < ANum) ? BNum : Anum and reads like:

if (BNum < ANum)
{
    sequence = BNum;
}
else
{
    sequence = ANum;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Gary.S
  • 7,076
  • 1
  • 26
  • 36