6 ms·
Some people in that discussion wonder about "->", which is used for indirect addressing through a structure member. When C has added structures, which did not
by adrian_b 3y ago
Some people in that discussion wonder about "->", which is used for indirect addressing through a structure member.
When C has added structures, which did not exist in B, it has taken the keyword "struct" and also both "." and "->" from the IBM PL/I language, from which C has also taken some other features.
In general, almost any feature added by C to B was taken either from PL/I or from Algol 68. The exceptions are "continue" and the generalized "for", which did not exist in any previous language.
(However the generalized "for" of C was a mistake, because it complicates the frequent use cases in order to simplify seldom encountered use cases. The right way to generalize "for", i.e. with iterators, was introduced by Alphard in the same year with C, i.e. in 1974.) (Compare "for (I=0;I<N;I+=5) {" of C with "for I from 0 to N by 5 do" or "for I in 0:N:5 do" of previous languages. C requires typing a lot of redundant characters in the most frequent cases.)
The oldest symbol for indirection through a pointer (in the language Euler, in January 1966) was a raised middle dot (i.e. a point). This was before ASCII and ASCII did not include the raised middle dot (U+00B7), so it was replaced by the most similar ASCII character, "*".
Euler had used "@" for "address of" and indirection was a postfix operator, as it should. Making "*" a prefix operator in B and C was a mistake, which forced the importing of "->" from PL/I, to avoid an excessive number of parentheses. Otherwise "(*x).y" would have been needed, instead of "x->y". With a postfix "*", that would have been "x*.y", and "->", would not have been needed.
In CPL, the ancestor of BCPL and B, indirection was implicit, like with the C++ references. Instead of having an "address of" operator, CPL had a distinct symbol for an assignment variant that assigns the address of a variable, instead of assigning its value.
- Reviving1514 3y agoWow today I learned B was a programming language and that C was indeed supposed to be a replacement for that. How interesting. Thank you!
- masklinn 3y ago> Otherwise "(*x).y" would have been needed, instead of "x->y". With a postfix "*", that would have been "x*.y", and "->", would not have been needed. I mean technically the compiler could just have been less stupid and made `.` auto-deref, it’s not like C has operator overloading so the LHS is either a struct or a pointer, not both.
- adrian_b 3y agoIt is not that simple, because in C you may chain a great number of prefix and postfix operators. It is frequent to have multiple "*", "[]", "." and "->". Now you have the simple rule of executing first the postfix operators from left to right, then the prefix operators from right to left. If some special rules had to be invented to avoid writing the parentheses in "(*x).y", after adding several "*", "[]" and "." it would become impossible to understand the right evaluation order. When all of "*", "[]" and "." are postfix, they are just executed in order from left to right, which is easy to understand. While sometimes auto-dereferencing a pointer would be convenient, in C you want frequently to get the value of a link, or to do pointer arithmetic. With auto-dereferencing, some new ways of using an "address of" operator would have to be invented, which are unlikely to be more simple than the current rules. Like I have said, the 2 languages that have introduced the concept of pointers were Euler, with explicit dereferencing, and CPL, with implicit auto-dereferencing. Both are valid ways to design a programming language, but in both case a lot of rules must be added to cover all the use cases, which would have to be more complex in the languages with auto-dereferencing, so these languages usually solve this problem lazily, by just prohibiting some uses, e.g. by prohibiting pointer arithmetic.
- renox 3y agoThe problem with postfix * for dereferencing is that it would look like a multiplication.. That's why I would have used a postfix @ for dereferencing.
- tsimionescu 3y agoI believe GP means that "a.b" should be enough for pointers as well, you don't need "a->b" or "(*a).b". It's not like pointers can have fields in C, so the meaning would have been unambiguous.
- adrian_b 3y agoSorry, I had posted a partial reply before finishing writing the complete one. As I have written above, a language with implicit dereferencing needs additional syntactic means for denoting the cases when the values of the pointers are needed, e.g. for doing pointer arithmetic, which is frequent in C. If dereferencing would have been made implicit, that would have required a large number of changes in the language. C++ has introduced pointers with implicit dereferencing, i.e. what C++ calls references, but due to the fact that the other syntax changes that are needed have not been made, because they would break compatibility, the C++ references can replace C pointers only in a subset of their uses.
- AnimalMuppet 3y ago> Compare "for (I=0;I<N;I+=5) {" of C with "for I from 0 to N by 5 do" or "for I in 0:N:5 do" of previous languages. C requires typing a lot of redundant characters in the most frequent cases. Um, "for (I=0;I<N;I+=5) {" is fewer characters than "for I from 0 to N by 5 do".
- adrian_b 3y ago"for I from 0 to N by 5 do" is the verbose variant, which is typical for a language like PL/I or Algol 68. The syntactically equivalent, but non-verbose, is "for I ∈ 0:N:5 ⟨", which beats easily C.
- jandrese 3y agoDo people have normally keyboards with a ∈ key?
- loup-vaillant 3y agoJust remove the ∈ and ⟨ characters, and make braces mandatory: for i 0:N:5 { // loop body } By the way, the same should have been done with if and while: optional parentheses, mandatory braces: if a == b { // code } while idx < end { // code }
- d-lisp 3y agoIt's possible that parsing a complex condition wrapped by parentheses was found more convenient.
- loup-vaillant 3y agoIt’s not. Not for humans (the extra parenthesis is just useless clutter), and not for machines (I’ve written parsers, both alternatives are as easy to implement).
- AnimalMuppet 3y ago
- jandrese 3y ago> that would have been "x*.y", and "->", would not have been needed. Arguably that syntax would have been prone to a lot of unintended pointer multiplication due to syntax error.
- loup-vaillant 3y agoOr we could use a different operator for pointer indirection and multiplication? There’s also the pointer/array conflation that could cause some confusion.
- tralarpa 3y ago> the generalized "for" of C was a mistake, because it complicates the frequent use cases in order to simplify seldom encountered use cases The worst offender is the quasi-obligatory "break" statement in "case" blocks. Fall-through cases are useful for things like lexers but probably not needed in 99.999% of other programs. I wonder how many millions of (wo)man hours have been wasted on debugging missing breaks. (yes, I know that linters exist)
- bluecalm 3y agoYou don't really need a linter. It's just a warning setting, see: -Wimplicit-fallthrough From the compiler docs. It's just not a problem. If you waste time once and learn about warning options in the compiler you will be better off anyway. It's like with assignment being an expression panic - something that maybe bites you once and then you learn to read the compiler warnings and it never causes problems again. If you don't want to read then there is: -Werror Available.
- kps 3y agoThat (also) came from BCPL. (BCPL had a separate `endcase` statement; `break` applied only to loops. It also had `docase ‹expr›`, which is the equivalent of a loop `continue`, re-entering the `switchon`.)
- vincent-manis 3y agoAs I recall, B was modelled on an early BCPL compiler that didn't have an ENDCASE statement, hence the overloading of break for exiting a switch block.
- cassepipe 3y agoNote that gcc and clang will warn you about fallthrough statements i you use -Wextra and that you have to either have insert a /fallthrough/ comment to make your intent clear. Pity the (wo)man who does not use -Wall -Wextra for (s)he is truly mistaken
- 3y ago
- fanf2 3y agoThe prefix * in C comes from prefix ! in BCPL. C’s weird a[b] == b[a] is also a hidden BCPLism. BCPL has infix ! as well as prefix ! so you write an array index expression like array!index instead of array[index]. Infix ! is commutative like addition and [] in C. You declare structures in BCPL by defining a constant for the offset of each member, so you can write object!MEMBER somewhat like C object->member. The semantics of -> in early C were very similar to BCPL infix ! except that members had types as well as offsets, but like BCPL there was nothing to tie a particular member to a particular structure as there is in modern C. There’s a certain elegance to BCPL’s syntax that you don’t get from prefix-only or postfix-only indirection operators. C might have been better if it had stuck closer to BCPL in this respect, but sadly * conflicts with infix multiplication. Some readers might also have used BCPL-style syntax for WIMP programming in BBC BASIC on RISC OS.
- teo_zero 3y ago> sadly * conflicts with infix multiplication. I don't think it does. There's no ambiguity in parsing any expression that uses it in both se se. It does conflict with division, though, because /* starts a comment!
- alexeiz 3y ago> The right way to generalize "for", i.e. with iterators, was introduced by Alphard in the same year with C, i.e. in 1974.) (Compare "for (I=0;I<N;I+=5) {" of C with "for I from 0 to N by 5 do" or "for I in 0:N:5 do" of previous languages. Really? How do you do the C's equivalent of `for(i = 0; i < N && j < M; i++, j++)` with your `for I from 0 to N` syntax? The for loop in C is extremely flexible and it captures the idea of the loop perfectly: there is the initialization block, the exit condition and the iteration. In other languages at that time the for loop was written in terms of a range, but without a strong range abstraction such loop is really primitive and limited in applicability.
- nyssos 3y ago> In other languages at that time the for loop was written in terms of a range, but without a strong range abstraction such loop is really primitive and limited in applicability. That's a good thing: it gives you a clear syntactic marker for "this loop definitely terminates". There's already a full power anything-goes looping construct: `while`.
- professoretc 3y agoBut `while` doesn't restrict the scope of the variable(s) to just the loop. I believe there was a proposal to add while(int i = 0; i < 100) { ... } as a valid syntax to C++ (which would restrict the scope of i to the loop), as you can already do if(int i = f(); i > 0) { ... } but it didn't go anywhere.
- justincredible 3y ago[dead]
- a_e_k 3y agoI'd just use a for with an empty update clause for that: for (int i = 0; i < 100; ) { ... }
- MrJohz 3y ago
- lowbloodsugar 3y ago>because it complicates the frequent use cases in order to simplify seldom encountered use cases. Putting complex logic in the three places of a for loop is something we all frown on now because of readability and bugs, but back then it was almost more common than just iterating from 1 to N. Same for dropping through case statements - frowned on now (rightly) because gotcha bugs, but used heavily. >Making "*" a prefix operator in B and C was a mistake C dominates for the same reason it is terrible: it gets shit done for some value of "shit". In this light, there are no mistakes. There are other languages that don't make these "mistakes" and, behold, nobody wrote the majority of the world's operating systems and software in them. I've used C when the viable options were hand-crafted assembly or C. There are no mistakes here, only practicalities.
- WalterBright 3y agoThe thing is, the `->` operator in C is completely unnecessary. `.` can be used instead. The compiler can distinguish by looking to see if the lvalue is a pointer or a value. The result makes it much easier to refactor code. Ever try replacing a value with a pointer in C? Arrghh.
- guenthert 3y agoRedundancy is a good thing. It helps humans to detect errors and they need all the help they can get.
- paulddraper 3y agoI think it is debatable on whether struct property access and pointer access should appear the same. * Performance. Pointer access is an order of magnitude more expensive. * Errors. Pointer access can fail (in particular, the NULL pointer).
- WalterBright 3y agoIt may be debatable, but in D we've used it for 20 years and problems with it have yet to crop up. And indeed, it makes refactoring code far easier.
- paulddraper 3y agoNot directly to syntax, but D null pointer safety is weird IMO. It checks array access bounds, but segfaults for null pointers. Seems an odd choice, and there are multiple discussions of people saying the same.
- WalterBright 3y agoNull pointers are automatically checked by the hardware (seg fault). Adding another runtime check is redundant. Yes, I know, adding a huge fixed offset to the pointer can push it past the null protected pages.
- 3y ago
- d-lisp 3y agoI have the feeling the generalized "For" is nice because it allows for multiple initializers in a compact manner, how does for I from 0 to N by 5 deals with this ? e.g. for(i=0,j=N.length;i<j;i++)
- bmicraft 3y agoThis should be equivalent: for i from 0 to N.length [by 1]