Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Are these two lines of C code for PIC16F1947 the same?

Status
Not open for further replies.
T

treez

Guest
Newbie level 1
Hello,
Our software engineer tells us that the following two lines are the same in every way. Is this true? Do they both execute the same?
(as you can see , one has "!=" and the other has "=!".
The C compiler is the free one that comes with microchip's MPLAB software.



if(eepromWriteCount != BACKUP_1_eepromWriteCount)

if(eepromWriteCount =! BACKUP_1_eepromWriteCount)
 

I think you can answer the question yourself applying average C language knowledge.

The first is a pure comparison, the second is an assignment changing the value of eepromWriteCount.

Even if eepromWriteCount is discarded, it's a different if expression argument. First is true if both variables are unequal, the second if BACKUP_1_eepromWriteCount is zero.
 

Agreed.... shoot the software engineer.....

!= means "if not equal do the following" (a comparison)
=! means "make it equal to the following if it isn't true" (assigns a value to eepromWriteCount of zero or one)

Brian.
 

Anyway, an assignment within a conditional expression is something totally not recommended, and would be expected the compiler issue a warning in this case, since it is not a standard implementation, which could be interpreted differently from one compiler to another.
 

Anyway, an assignment within a conditional expression is something totally not recommended, and would be expected the compiler issue a warning in this case, since it is not a standard implementation, which could be interpreted differently from one compiler to another.
I almost agree, except for a potentially different interpretation.

IMHO, the C language specification is crystal clear in this relation: an assignment is an assignment, the if statement is evaluating the assigned value. The compiler should issue a warning though, because the assignment is possible inadvertent.

Writing if (a = b) instead of if (a == b) is a pretty popular fault.
 

I wasn't suggesting it was good programming practice to use it at all, especially in an 'if' statement.

"if(a == !b)" reads as "if a is equal to the inverted state of b".
"if(a != b)" reads as "if a is not equal to b".
"if(a =! b) reads as "assign the value to a of the inverted state of b"., the 'if' is ambiguous as no query is made inside the braces.

However in the first post the implication is that both a and b are numeric values rather than boolean. I think the line is intended to ask if the working and backup values are different so "!=" is the operator to use.

Brian.
 

"if(a =! b) reads as "assign the value to a of the inverted state of b"., the 'if' is ambiguous as no query is made inside the braces.
IMHO it's bad programming style, but not ambiguous. It's just a shortcut of

Code C - [expand]
1
2
a = !b;
if (a) ..

 

However in the first post the implication is that both a and b are numeric values rather than boolean. I think the line is intended to ask if the working and backup values are different so "!=" is the operator to use.
Thanks yes the software guy now wants to use "!="

The following of his code indeed shows that they are not boolean types, so yes i suppose it would be a mystery how the compiler would deal with this "=!"..


// EEPROM VAR (part of eeprom where data change often)

volatile int32_t eepromWriteCount; // number of writes for this block

volatile int32_t BACKUP_1_eepromWriteCount;
 

The following of his code indeed shows that they are not boolean types, so yes i suppose it would be a mystery how the compiler would deal with this "=!"..
Again, it's not a mystery, the C standard is crystal clear, both about the comparison "!=" and the logical not "!" applied to numerical types. Most of this thread discussion can be safed by reading a C text book like Kernighan & Ritchie.

As for the "!" operation, the result is 1 if the argument is equal to zero else 0.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top