CCss1
CCss1

Reputation: 31

Some confusion with regards to parenthesis in C

Please bear with me, I am trying to learn C as my first programming language and am only 15 minutes in.

Why must parenthesis be used here:

while ((number1 = number2))

...when they do not need to be used here?

while (number1 <= number2)

Thanks in advance.

Upvotes: 2

Views: 203

Answers (5)

John Bode
John Bode

Reputation: 123468

It is a very common mistake to write

if ( a = b ) // assign b to a, branch if result is non-zero

when you meant to write

if ( a == b ) // *compare* b to a, branch if equal

leading to all kinds of mayhem.

It's such a common mistake that most compilers will issue a warning if they see an assignment in a conditional like that. To tell the compiler, "no, I really know what I'm doing", you surround the assignment in an additional set of parentheses:

if ( ( a = b ) )

This basically means, "yes, I intend to assign b to a and branch on the result, shut up."

If I could time travel back to Bell Labs in 1970, this is one of several decisions I'd slap Ritchie over. An entire class of bugs would never have existed if the assignment operator had been := or something equally dissimilar to comparison.

Upvotes: 2

user2736738
user2736738

Reputation: 30926

Who said there is a must? You can omit it too.

while (number1 = number2) 

yes this would generate compiler warning because you are assigning and checking the assigned value without parentheses. But this is legal and bad practice.

So the reason they did this is to avoid the warning about which the compiler complained. (You shouldn't skip warning to follow a bad practice - rather allow compiler warnings and try to solve them diligently).

Though as you are beginner - aware of the statement, here the while statement is basically

  while(number2)

Point: The most common to use would be to while(number1 == number2).


Also does the same thing as the previous one. Who said in the second one you can't? You can.

while ((number1 <= number2))

Upvotes: 1

nemequ
nemequ

Reputation: 17482

First off, the first example is extremely bad practice. It's not comparing number1 and number2, it's setting the value of number1 to the value of number2. See https://freedom-to-tinker.com/2013/10/09/the-linux-backdoor-attempt-of-2003/ for some more information.

That said, both forms are allowed; you can always add extra parenthesis. In the case you have above, there is no difference.

That said, odds are good that you're simplifying from an example where multiple conditions are chained together, like

while ((number1 == number2) || (number3 == number4))

As you can see, it makes it a bit easier to see what the code is doing. Technically it's not really necessary here, either (though it's generally considered to be good practice), but it can be necessary for more complicated expressions because different expectations about operator precedence can result in your code doing things you might not expect. To take an example from that page, for something like

e = a < d ? a++ : a = d;

It can take a few moments even for an expect to figure out what is going on, and someone less familiar with C will probably need much longer. On the other hand, if you add parenthesis:

e = ( ((a < d) ? (a++) : a) = d );

Things become much easier to read.

Upvotes: 0

CDove
CDove

Reputation: 1950

This is a matter of opinion. Many programmers will still wrap a comparison in parentheses for clarity. That's to make them more legible. For instance:

while(i == 0)   //comparison
while(i = 0) //assignment (and therefore, infinite loop)

but more commonly, because chains of conditions can become very hard to read, and getting into the habit of parenthesizing your comparisons makes complex comparisons easier to understand:

    //whiskey tango foxtrot
    while(i == 0 || j == i && k == 5 || f == "pancakes")
    //oh, now it's clear that we're not checking "i && k". Easier to read! 
    while((i == 0) || (j == i) && (k == 5) || (f == "pancakes"))
    //now I've guaranteed i and j both have the value "0" by using parentheses, 
    //or we get false; but everything is still clearly separated.  
    while(((i == 0) || (j == i)) && (k == 5) || (f == "pancakes")) 

Upvotes: 0

jfMR
jfMR

Reputation: 24738

In:

while (number1 = number2)

number2 is being assigned to number1.

This is optically very similar to comparing number1 and number2, i.e.:

while (number1 == number2)

So a warning is produced in the former case. In order to suppress that warning you need to place the parentheses around the assignment, i.e.:

while ((number1 = number2))

Upvotes: 2

Related Questions