Double equals in C

ApacheKid

Joined Jan 12, 2015
1,762
I can't think of an application in C where using = deliberately in an if statement is in any way consistent with writing clear and easy-to-follow software.
I agree, the language (and its many derivatives) is littered with silliness like that.
 

nsaspook

Joined Aug 27, 2009
16,373
I can't think of an application in C where using = deliberately in an if statement is in any way consistent with writing clear and easy-to-follow software.
Something's in software that directly interfaces with hardware are neither clear, well structured or easy to follow. That's what code comments are for.
 

WBahn

Joined Mar 31, 2012
32,998
I can't think of an application in C where using = deliberately in an if statement is in any way consistent with writing clear and easy-to-follow software.
Examples were given earlier in the thread.

As with any notation, it looks odd and awkward at first, but once you gain a level of comfort and familiarity with it, it becomes much more clear and easy to follow. The same is true for the notation for logical operators, for the distinction between them and bitwise operators, and especially for something like the ternary operator.
 

MrChips

Joined Oct 2, 2009
34,970
One should not be too pedantic over the meaning of an innocent looking expression such as:

a = a + 1

In other programming languages, it could take on a much different meaning. For example, what would be the outcome if a were a character string, array, matrix?
 

ApacheKid

Joined Jan 12, 2015
1,762
Would you prefer Pascal?
a := a + 1

or APL?
a ← a + 1
These are options of course. C was created at a time when ASCII (and IBM's EBCDIC) was the only choice, today we have Unicode so the APL-like arrow is now a no-brainer. I designed a grammar a year ago, and incorporated optional Unicode symbols for several operators, like rotate left/right, assignment ( ⇐ ), XOR and so on, because there's no longer any good reason not to do so.

https://github.com/Steadsoft/imperium/blob/main/grammar/Antlr/ImperiumLexer.g4#L231
 
Last edited:

ApacheKid

Joined Jan 12, 2015
1,762
One should not be too pedantic over the meaning of an innocent looking expression such as:

a = a + 1

In other programming languages, it could take on a much different meaning. For example, what would be the outcome if a were a character string, array, matrix?
Yes you're right, we shouldn't be pedantic, we're all used to this too by now. This reminds me though of a gripe I have about many assembly languages. Too many use MOV when in reality COPY is a better term, I've had intelligent people literally get confused, thinking that MOV implies the bits go from one place to another with the source bits then getting automatically set to all zeroes, COPY would be so much better.
 

MrChips

Joined Oct 2, 2009
34,970
Have a look at the APL operators. APL was invented by a mathematician Kenneth E. Iverson in the 1960s using mathematical notation.

For example,
!A is factorial of A
A ! B is number of combinations of B in A.
 

ApacheKid

Joined Jan 12, 2015
1,762
Have a look at the APL operators. APL was invented by a mathematician Kenneth E. Iverson in the 1960s using mathematical notation.

For example,
!A is factorial of A
A ! B is number of combinations of B in A.
I'm a huge admirer of APL and Iverson, like LISP every aspiring programmer should spend time with APL to enhance how one perceives problems, data and solutions. I used APL regularly in the 1980s, it was available on our IBM mainframe with dedicated keyboard too.

Have you seen this?

https://tryapl.org/?tab=3
 

ApacheKid

Joined Jan 12, 2015
1,762
Something's in software that directly interfaces with hardware are neither clear, well structured or easy to follow.
Like what? C often obfuscates one's intent, makes the simple look complex, take the language's approach to strings for example, concatenation is silly.

In an MCU all one can typically do is read/write to peripheral registers, that's it, read/writes from/to memory, what's not clear in that paradigm? it's just data like reading/writing to a DB or comms channel or whatever.
 
Last edited:
Top