# [SOLVED]Difference between (Assignment) and (Equal to) operators.

#### Pushkar1

Joined Apr 5, 2021
205
I'm little bit confused between (Assignment) and (Equal to) operators. I don't understand which specific operator should be use and when it should be use

Here I've written two case.

Equal operator
C:
if (PIN1 == 1)
{
do_something();
}
Assignment operators.
Assignment operator
C:
if (PIN1 = 1)
{
do_something();
}
How do you use both operator in embedded project ? Please help me to understand specific use

#### Yaakov

Joined Jan 27, 2019
3,464
The first checks the value of PIN1, the it is equal to 1 the expression evaluates as TRUE and the code is executed.
The second attempts to assign the value 1 to PIN1, if it succeeds, the expression evaluates as true, and the code is executed.

The second is an error, it is exceedingly unlikely you want to test the assignment. There may be an edge case that calls for it, but it is almost certainly not correct wherever you see it.

As an aside, if for some unknown reason you actually wanted that code, it is the perfect example of a place to put a comment since 99.999% of the time it will be an error.

#### BobTPH

Joined Jun 5, 2013
3,648
Let’s not confuse things by putting the assignment in an if statement. The fact that you can do that is a quirk of the C language and is not allowed in most other languages.

Why don’t you try to explain what this code means:

Code:
int x;
x = 1;
Bob

#### Yaakov

Joined Jan 27, 2019
3,464
I want to add a bit.

The equivalence operator tests what is on each side of the == and if they are equal, according to the evaluation rules of the language, returns TRUE, if not, then FALSE. When you use it in a conditional structure, it allows you to execute code based on the truth or falsehood of the statement. It is a test, an inspection of the two sides.

The assignment operator, on the other hand, is an action. It assigns a value to a variable. That variable could be tested with == later, but the return from - is success or failure of the operation of assignment.

#### Pushkar1

Joined Apr 5, 2021
205
Why don’t you try to explain what this code means:
Bob
It's simple. look at comment
Code:
int x;  //variable name is x. Type of integer
x = 1; // x is set to 1
Confused with below logic

C:
if (PIN1 = 1)
{
do_something();
}
have you used assignment operator inside if-statement in any project?

#### Ian Rogers

Joined Dec 12, 2012
915
The if statement ONLY needs to know TRUE / FALSE so anything other than 0 is true..

if ( 1 ) is true
if ( pin = 1 ) is true ( as pin = 1 )..
if( pin == 1 ) only true if pin = 1...

In pascal they get around this with := for assignment and just = for equality..
Even seasoned professionals get bit by this frequently...

#### BobTPH

Joined Jun 5, 2013
3,648
As I said. The use of the assignment operator in an if statement or any other expression is a quirk of the C language and should never have been allowed. Do not use it that way.

Let's see if you understand if statements now.

Code:
if (x + y)
{
do_something();
}
What does this do?

Bob

#### MrChips

Joined Oct 2, 2009
24,134
Going one step further, find out what would be the outcome of this statement:

if ( x = a + b )

#### Pushkar1

Joined Apr 5, 2021
205
As I said. The use of the assignment operator in an if statement or any other expression is a quirk of the C language and should never have been allowed. Do not use it that way.

Let's see if you understand if statements now.

Code:
if (x + y)
{
do_something();
}
What does this do?

Bob
I know any None zero value inside if statement will be always true
C:
#include<stdio.h>

int main ()
{
int x, y = 1;

if ( x + y )  //  None zero value will be always true
printf("TRUE");

else

printf("FALSE");

return 0;
}
Result : TRUE

#### Pushkar1

Joined Apr 5, 2021
205
Going one step further, find out what would be the outcome of this statement:

if ( x = a + b )

C:
#include<stdio.h>

int main ()
{
int x;
int a = 1; int b = 1;

if ( x = a + b)  //  None zero value will be always true
printf("TRUE");

else

printf("FALSE");

return 0;
}
Result : TRUE

#### Pushkar1

Joined Apr 5, 2021
205
Which operator "assignment or equal to" do you use inside interrupt routine ?

Pesudo code Timer interrupt

C:
void main ()
{
System_Initialize();
setUp_TimerInterrupt();

while (True)
{
if (flag == 1)
LED = ON;
Wait(1000)
LED = OFF;
Wait(1000)
}
}

ISR ()

{
if (TimerFlag == 1;
TimerFlag = 0;
flag == 1;
}

#### Ian Rogers

Joined Dec 12, 2012
915
Interrupts are no different to normal functions... Same rule applies.. That last statement is incorrect

#### Pushkar1

Joined Apr 5, 2021
205
Interrupts are no different to normal functions... Same rule applies.. That last statement is incorrect
@Ian Rogers Which statement is incorrect and why do you think that's incorrect?

#### Ian Rogers

Joined Dec 12, 2012
915
Which statement is incorrect and why do you think that's incorrect?
Not incorrect perse but
C:
ISR ()
{
if (TimerFlag == 1;
TimerFlag = 0;
flag == 1;  // <-- doing absolutely nothing   same as delay_us(1);
}
[code]

#### MrChips

Joined Oct 2, 2009
24,134
Do you understand how this statement is implemented and hence what it does?

if ( x )

Can you see any difference between the above statement and this one?

if ( x = y )

#### djsfantasi

Joined Apr 11, 2010
7,823
Not incorrect perse but
C:
ISR ()
{
if (TimerFlag == 1;
TimerFlag = 0;
flag == 1;  // <-- doing absolutely nothing   same as delay_us(1);
}
[code]
Will this compile? It has mismatched parens…

#### Ian Rogers

Joined Dec 12, 2012
915
Yup... The line flag == 1; will execute always.. But it does nothing... ( I just copied his )

C is unforgiving.... Most languages will throw warnings... C will allow but you will have a bug...

#### click_here

Joined Sep 22, 2020
374
I think I see where the confusion may be creeping in.

An "if" statement is a "selection" statement which will be executed if the expression equates to a value unequal to 0.

Both of these will execute:
Code:
apple = 5;
...
if(apple==5)
{
...
}

...

banana = 5;
...
if(banana=3)
{
...
}
"banana=3" actually has the result of "3" (which is not 0), so it excecutes, but the value that was in banana is now lost and replaced with the value 3.

This is a bug.

A way of guarding against this bug is by using "yoda conditions" - i.e. if I forget the second "=" it throws an error...
Code:
if(5==apple) //okay
{
...
}

if(5=apple)  //Error
{
...
}
However, most modern compilers will warn you if you have used only 1 = sign -> You'd have to put it in another set of brackets if that was what you wanted, just to tell the compiler that is what you wanted (or just ignore the error, which is never wise...)

A common example of this using a "while" statement...
Code:
while( (ch = getchar()) )
{
...
}