# operator precedence

``````  #include <stdio.h>
int main(void) {
char t[] = { 'a', 'b', 'A', 'B' };

printf("%d",t[1] - t[0] + t[3] - t[2]);
return 0;
}
``````

2 operators + and -
precedence + higher than -
left-right

a=97
b=98
A=65
B=66

applying precedence rule t[1]-(t[0]+t[3])-t[2]=t[1]-163-t[2]
98-163-65=-130

how compiled program is giving me 2 ?

EDIT: I can get 2 only if I will replace precedence by - to be higher than +

Check it out in assembler. Donuts to dollars the code is grouped at the ends. The compiler doesnt know what the hell so its applying group logic. This may not even be in the docs.

Hi, What IDE, model Arduino are you using?

Can you show the whole code so we can run it ourselves?

Tom... :)

Where do you see that + has precedence over -? It says here that they are equal which would (using left to right) give you 2.

https://en.cppreference.com/w/cpp/language/operator_precedence

Hi I tried this;

``````/*
a=97
b=98
A=65
B=66
*/
char t[] = { 97, 98, 65, 66 };

void setup()
{
Serial.begin(9600);

Serial.print(" t[1] - t[0] + t[3] - t[2] =\t");
Serial.println( t[1] - t[0] + t[3] - t[2]);

Serial.print("\n t[1] - (t[0] + t[3]) - t[2] =\t");
Serial.println( t[1] - (t[0] + t[3]) - t[2]);
}

void loop()
{

}
``````

Arduino Nano, IDE 1.8.7

Result;

t[1] - t[0] + t[3] - t[2] = 2 t[1] - (t[0] + t[3]) - t[2] = -130

Unless you are talking about something that is way above my head.

Delta_G: Where do you see that + has precedence over -? It says here that they are equal which would (using left to right) give you 2.

https://en.cppreference.com/w/cpp/language/operator_precedence

I`m using C reference not CPP but noticed its same

1. a*b a/b a%b Multiplication, division, and remainder Left-to-right

its written as it should be in your link first * is higher than / and / is higher then %

so if we will compile 2*2/2 compiler will do 2*2 and then /2 we will get 4

line 6 in reference stays a+b a-b so a+b has higher precedence than a-b same as line 5 and other lines where its starting with higher precedence operator first then is going to lowest in that range.

may be I`m not reading that reference right ?

EDIT: Delta_G you are right! they are equal so only left to right rule applies. Thanks and Merry Christmas!

surepic:
I`m using C reference not CPP but noticed its same

1. a*b a/b a%b Multiplication, division, and remainder
Left-to-right

may be I`m not reading that reference right ?

Right. Items on the same line are equal precedence with each other. The left-to-right is not saying multiplication has higher precedence than division. What the chart is telling you is multiplication has higher precedence than addition/subtraction. Same as in algebra.

Operators are listed top to bottom, in descending precedence.

You can enforce the precedence you want by grouping with ().

surepic: so if we will compile 2*2/2 compiler will do 2*2 and then /2 we will get 4

I never get 4 out of that expression, regardless of the order of operations.

``````(2 * 2) / 2  =  4 / 2  =  2
2 * (2 / 2)  =  2 / 1  =  2
``````

@Whandall have absolutely no idea from where that 4 came from :-)))

But you recognized where the negativ result of the character expression had its origin?

surepic: applying precedence rule t[1]-(t[0]+t[3])-t[2]=t[1]-163-t[2]

``````t[1] - t[0] + t[3] - t[2]

== t[1] + (-t[0] + t[3]) - t[2]
== t[1] - (t[0] - t[3]) - t[2]

!= t[1] - (t[0] + t[3]) - t[2]
``````

Yes, there was a mistake when you inserted the brackets.