What does this statement mean...

Go To Last Post
8 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi

This is a statement from some accelerometer code in the EVK1101 fromAtmel ASF

I am running the example code and I can see that it is to display the angle (that the board is held at)

But I am not quite sure about the syntax of this statement:

  if( 0!=(res=is_acc_abs_angle_x(60)) ) angle.x = 60;

In particular if (0!=  .... I have not seen before

 

thanks

all

Russell

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

1. the return value from is_acc_abs_angle_x()is assigned to res

2. if res ( and the return value), does not equal zero  (!= 0 )

    then assign 60 to angle.x

Keith Vasilakes

Firmware engineer

Minnesota

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for the quick reply

Russell

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

russellsher wrote:
In particular if (0!=  .... I have not seen before

Well, '0' is just zero; and you must be familiar with "!=" as the "is not equal to" operator - surely?

 

I guess you're more used to seeing it as:

  if( (res=is_acc_abs_angle_x(60)) != 0 ) angle.x = 60;

But, clearly A != B is exactly equivalent to B != A - the order is unimportant

 

So why do people do it "backwards"?

 

The reason is that it is a very common mistake to write

  if( something = 5 )

when

 

  if( something == 5 )

is required.

 

The trouble is that it is perfectly valid 'C'; so compiles without error - but doesn't do what you wanted.

 

By writing it "backwards", we get

  if( 5 == something )

And accidentally substituting '=' for '==' is then not valid:

  if( 5 = something )

Of course, the problem never arises with "!=" - so it's done there just to be consistent.

 

http://c-faq.com/style/revtest.html

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Wed. Mar 18, 2015 - 08:08 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

But embedding the assignment inside the 'if' clause is entirely pointless.

 

There is absolutely no advantage whatsoever over writing

 

res = is_acc_abs_angle_x(60);
if( 0!= res ) angle.x = 60;

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:

But embedding the assignment inside the 'if' clause is entirely pointless.

 

There is absolutely no advantage whatsoever over writing

 

res = is_acc_abs_angle_x(60);
if( 0!= res ) angle.x = 60;

 

 

agree with that!

And I think it's worse than pointless as it's confusing.

Confusing is bad

Keith Vasilakes

Firmware engineer

Minnesota

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I agree - and there are further reasons why it's worse than pointless:

 

  • It makes the expression unnecessarily complex.
    Unnecessary complexity is bad.
     
  • It makes it harder to debug, as the res value is harder (or impossible) to access.
    Difficult-to-debug is bad.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Gentlemen,

 

I've never seen this formula using in an 'if' statement before, but many times in while().

Anyway +1 to should be avoided.

 

BR,

M