Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Bug in Arduino compiler.

Pommie

Well-Known Member
Most Helpful Member
I recently had a bug in my program and couldn't figure it out.
Turns out I'd typed count==25; instead of count=25;
I thought this would be an error or at least a warning. It isn't.

As an experiment I placed the following in a new sketch,
Code:
void setup(){
  uint8_t count;
  count==25;
  Serial.begin(115200);
  Serial.println(count);
}

void loop(){}

It compiles fine but count is displayed as 0.

Mike.
Edit, is this normal compiler behaviour? Just tried it in XC8 with the same result.
 
I know the difference, I was expressing my surprise that it doesn't generate an error or warning.

Mike.
 
I am not sure about this, from an ANSI C point of view, but the ==
statement does not have to be in parenthesis so compiler cannot
figure out what its supposed to be. And vice versa, if its single =
is it supposed to be test, a == ?

Maybe a C expert can comment on this.


Regards. Dana.
 
I just consider it a syntax error and it should be flagged as such.

I'm just wondering if there is a construct along the lines of strcpy(buff,count==25?"is 25":"isn't") that I'm not aware of.

Mike.
 
It is not a syntax error. As written, it is syntactically correct. It is a logical expression that evaluates to TRUE or FALSE. It is semantically wrong, because a free-standing logical expression has no effect on the overall program. I do agree that a "smart" compiler could throw a warning for useless expressions masquerading as statements. A really smart compiler would just optimize it away into nothingness and maybe mention the fact that you're being wasteful by coding free standing expressions that have no effect.
 
I just consider it a syntax error and it should be flagged as such.

It's not a syntax error, it's perfectly correct C code - so can't be flagged as a syntax error (and yes, I do it a LOT as well :D )

I'm just wondering if there is a construct along the lines of strcpy(buff,count==25?"is 25":"isn't") that I'm not aware of.

Mike.

If you write it slightly differently though, it WILL trigger an error:

'25==count' does exactly the same, but '25=count' generates an error (because you're writing to a constant).

If you look at MCC created code, it's often done that way.
 
Using 25==count makes sense when used as a comparison but using an assignment backwards doesn't make sense. You are simply introducing a deliberate error!

Note, the construct I wrote, strcpy(buff,count==25?"is 25":"isn't") is valid in C.

I was wondering what use count==25 might have (if any).
and yes, I do it a LOT as well :D

I have only ever done this once. I often do if(count=25) (which I suspect you mean) but that's a completely different problem.

The problem is a line which only contains count==25; is valid in C but I've no idea why.
XC8 does the same thing if(x=5) gives a warning but x==5; doesn't!!!

Mike.
 
Caught me out a few times too, Should be an error I guess. what about .. if(count=2) .... not sure XC8 errors ?
In that case xc8 gives a warning.
warning: using the result of an assignment as a condition without parentheses [-Wparentheses]

But not if you type (only) LATA==0; It just compiles to nothing.

Mike.
 
Using 25==count makes sense when used as a comparison but using an assignment backwards doesn't make sense. You are simply introducing a deliberate error!

Note, the construct I wrote, strcpy(buff,count==25?"is 25":"isn't") is valid in C.

I was wondering what use count==25 might have (if any).


I have only ever done this once. I often do if(count=25) (which I suspect you mean) but that's a completely different problem.

The problem is a line which only contains count==25; is valid in C but I've no idea why.
XC8 does the same thing if(x=5) gives a warning but x==5; doesn't!!!

Mike.
It is valid BECAUSE any expression is a valid statement period FULL STOP.
This is similar to x++; which is a valid expression that can turn into a statement. This is also similar to a function call, is evaluated as an expression and returns a value. They are all syntactically the same, but semantically different.

A smart compiler that realizes that an evaluated expression has no effect is free to optimize the evaluation away without effecting the operation; it assumes you knew what you were doing, which is not always the case. If it was really benevolent it might throw a warning to draw your attention to the matter.
 
Last edited:
Yes, still doesn't help catching a typo. Most other typos I can think of result in an Error or Warning.
Think I'll look for a lint type program.

Mike.
 
Yes, still doesn't help catching a typo. Most other typos I can think of result in an Error or Warning.
Think I'll look for a lint type program.

Mike.
I'll be curious to learn what you find. I've never had the need to take such an action.
 
I'll probably wait until next time I've got a bug I can't find. However, forewarned is forearmed. As I said above, I've only done this once.

Mike.
 
I'll probably wait until next time I've got a bug I can't find. However, forewarned is forearmed. As I said above, I've only done this once.

Mike.
I cross my fingers in your favor and hope for the best result possible.
 

Latest threads

New Articles From Microcontroller Tips

Back
Top