A number of languages use == for equality comparison to disambiguate it from = for assignment. Other languages use a different symbol, such as := or -> for assignment.I was just wondering what benefits the == test for equality bring to the C language over the single = that other languages (Basic, for instance) use.
if (fp = fopen(filename, mode))
// The file opened successfully
else
// The file failed to open
fp = fopen(filename, mode);
if (NULL == fp)
exit(EXIT_FAILURE);
fp = fopen(filename, mode);
if (fp = NULL)
exit(EXIT_FAILURE);
I do assignments during conditional tests as a shortcut (test and assign) to save the result (usually from a function) for later possible use.I suppose that in order to answer my question, I would first have to understand the advantages of doing an assignment as part of a conditional test. So far, I have only seen that as a way of creating confusion between assignment and equality test.
if (n = foo())
{
/* foo returned a non-zero value, do something with the return value */
} else {
/* foo returned zero, do something else */
}
That's smart. I often wondered why I saw that in programs.if (5 == x)
Because now, if you make the mistake and use = instead of ==, you have made a syntax error.
Big difference. This is a common programming error that you don't want to make.I was just wondering what benefits the == test for equality bring to the C language over the single = that other languages (Basic, for instance) use.
I gave an example.I suppose that in order to answer my question, I would first have to understand the advantages of doing an assignment as part of a conditional test. So far, I have only seen that as a way of creating confusion between assignment and equality test.
c = readchar();
while (c)
{
// Do whatever with c
c = readchar();
}
while (c = readchar())
{
// Do whatever with c
}
while ('\n' != (c = readchar()))
{
// Do whatever with c
}
It's easy to parse, no benefit to the programmer, take a look at Javascript, there you can use === I can't wait for ==== one day.I was just wondering what benefits the == test for equality bring to the C language over the single = that other languages (Basic, for instance) use.
It's not because it's easy to parse. The semantics are very different. If it were just a parsing convenience, you could use a single equals sign for both operators and the code would still only have one valid interpretation. But that's not the case, because in many, even most, contexts where you could use one you could use the other and have valid, but semantically different, code. Just because YOU don't like it and don't want to leverage it, does not mean that it has no benefit to the programmer.It's easy to parse, no benefit to the programmer, take a look at Javascript, there you can use === I can't wait for ==== one day.
Well it is easier to parse, so I suspect that played a role in it being chosen. In the 60s compilers were hard to develop, no tools and often mainly written in assembler.It's not because it's easy to parse. The semantics are very different. If it were just a parsing convenience, you could use a single equals sign for both operators and the code would still only have one valid interpretation. But that's not the case, because in many, even most, contexts where you could use one you could use the other and have valid, but semantically different, code. Just because YOU don't like it and don't want to leverage it, does not mean that it has no benefit to the programmer.
Assignment could simply be a <- b;I don’t like it either: it catches me out frequently*. I grew up with BBC Basic, but I didn‘t think that it way that was just because some misanthrope was having a bad day when C got invented.
However, having followed the discussion I don’t see many examples of assignment during a conditional test that are consistent with the writing of clear code. I can see the value when memory was scarcer, processors were slower and compilers weren’t so clever.
* I’m going to try
if (CONSTANT == variable)
to see if I can get it wrong less often, but often it is better to test for
variable>CONSTANT
especially with counters, as that catches any faults where the counter has got out of bounds.
Bear in mind too, the semantics of a DOT are very different too:It's not because it's easy to parse. The semantics are very different. If it were just a parsing convenience, you could use a single equals sign for both operators and the code would still only have one valid interpretation. But that's not the case, because in many, even most, contexts where you could use one you could use the other and have valid, but semantically different, code. Just because YOU don't like it and don't want to leverage it, does not mean that it has no benefit to the programmer.
When testing for limits, you should not test for equality. Use <= and >= instead of ==.I don’t like it either: it catches me out frequently*. I grew up with BBC Basic, but I didn‘t think that it way that was just because some misanthrope was having a bad day when C got invented.
However, having followed the discussion I don’t see many examples of assignment during a conditional test that are consistent with the writing of clear code. I can see the value when memory was scarcer, processors were slower and compilers weren’t so clever.
* I’m going to try
if (CONSTANT == variable)
to see if I can get it wrong less often, but often it is better to test for
variable>CONSTANT
especially with counters, as that catches any faults where the counter has got out of bounds.
Never use "never"....never use ==.