Two functions, one works one doesn't

Hi,

I've got two bits of code that should do the same thing, but one of them works and one doesn't.

This version works....

uint16_t funcA( uint16_t n )
{
  uint16_t z=-n;
   
  return z & 7;
}

This version should do exactly the same....

uint16_t funcB( uint16_t n )
{
  return –n & 7;
}

But instead I get this compiler error.....

Example1:168: error: stray '' in program
Example1.cpp: In function 'uint16_t f4(uint16_t)':
Example1:168: error: 'u2013n' was not declared in this scope

Any idea why the first works but the 2nd doesn't?

BTW, the 2nd non-functioning example is the original code that seems to work on a different implementation of C. The 1st example does do what I need it to do, so this question is for information only, I already have a viable solution.

Thanks

How are you using them because this works just fine.

(IDE 1.5.7)

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println(funcA(50));
  Serial.println(funcB(50));
}

void loop() {
  // put your main code here, to run repeatedly:

}

uint16_t funcB( uint16_t n )
{
  return -n & 7;
}

uint16_t funcA( uint16_t n )
{
  uint16_t z=-n;
   
  return z & 7;
}

Hello,

Your errors aren't related to those functions you posted. If you try in a blank sketch what happens? Post your actual code :wink:

I'm using version 1.0.

This is my full code.....

uint16_t f4( uint16_t n )
{
  return –n & 7;
}

And my full error message....

sketch_dec16a:2: error: stray '\' in program
sketch_dec16a.cpp: In function 'uint16_t f4(uint16_t)':
sketch_dec16a:2: error: 'u2013n' was not declared in this scope

That's impossible...

This is my full code.....

No setup() function ?
No loop() function ?
Not even main() ?

Perhaps you should go back to the basics of using an Arduino and its software.

I can replicate this error by just writing

–n

in a totally empty sketch. It isn't a minus sign - !

That's impossible...

Thank you. I have always want to achieve the impossible, I'm very proud!

No setup() function ?

correct

No loop() function ?

correct

Not even main() ?

correct again!

In an effort to dot the i's and cross the t's I have added a loop() and setup() and have attempted to attach a screen dump. The error message remains the same.

Hi, do you know how to program, especially C++ and in the arduino environment?
What are you trying to accomplish?

I ask these questions so we don't find we are wasting our time.

Tom....... :slight_smile:

That's a very old version of the IDE. Try downloading a more current version and it compiles just fine.

Try downloading a more current version and it compiles just fine.

Yes - I've just discovered that myself.

Unfortunately the later version of the the IDE incorporate some changes in the way PROGMEM is handled. The result is that several of my older projects only work on version 1.0, hence my reluctance to do a permanent upgrade.

What are you trying to accomplish?

I ask these questions so we don't find we are wasting our time.

Both functions do the same thing, they return the compliment to the next multiple of 8. Since one of them works, I do already have a solution. As stated in my original post, the problem was posted for information only, it now seems that the problem was to do with the IDE/compiler, rather than a programming issue.

Thanks for everyones input.