Timeline for Determining if a C-style string is alphabetic
Current License: CC BY-SA 3.0
7 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Mar 29, 2018 at 1:32 | comment | added | user7649 |
Actually, I,m pretty certain the !! is vital in the original code, because OP is using &= (bitwise & rather than logical &&) and the standard only requires that the character classification functions return zero or non-zero. The !! forces the output of isalpha() to become zero or one so it works correctly with &.
|
|
| Mar 28, 2018 at 17:13 | comment | added | esote | I really like this solution, it is very simple (although maybe less readable). | |
| Mar 28, 2018 at 16:03 | comment | added | Baldrickk | I would wonder what @Ilmari is up to that this would make a difference. To 99.9% of all C programmers optimising their code for ~40 year old compilers is pointless | |
| Mar 28, 2018 at 14:32 | comment | added | Ilmari Karonen |
@Lundin: A sufficiently dumb compiler from 40 odd years ago might actually generate different (and possibly less efficient) assembly for your version than for vnp's. That said, I'd expect an actual optimized implementation targeting compilers from that era to look something like this: register char c, *p; do { c = *p++; } while (isalpha(c)); return !c; And I'd at least consider replacing the isalpha() call with a hand-written macro.
|
|
| Mar 28, 2018 at 8:22 | comment | added | Lundin |
For example something along the lines of while (isalpha(*c)) { c++; } return *c != '\0';. This is CR, not IOCCC.
|
|
| Mar 28, 2018 at 8:16 | comment | added | Lundin |
I wouldn't really recommend anyone to write obscure code like this. The only purpose it fills is to stroke the programmer's ego, making them look "smart". Why not write return !- true<:c:>; while you are at it... Instead, write code as readable as possible.
|
|
| Mar 28, 2018 at 4:48 | history | answered | vnp | CC BY-SA 3.0 |