Live data from Hacker News

First C compiler ported to GCC

github.com

41–50 of 90 posts

Re: First C compiler ported to GCC

#41
post #39
post #36

I do not understand why we should create a C compiler ported to GCC.

Not a C compiler. This is the original C compiler from ~1972. It's just an experiment to bring a bit of computing history to life.

Oh! At first I did not understand but now looks reasonable thanks

Re: First C compiler ported to GCC

#42
post #21

The a[b] implemented as *(a+b) Thing, is how we were taught to think about array indexing in the CS lectures of the 70s

And that's how it's still taught nowadays. Both the C89 and the C99 standard draft contain the following: > The definition of the subscript operator [] is that E1[E2] is identical to (*((E1)+(E2))) In fact the expressions a[b] *(a + b) and b[a] are equivalent. Here is a perfectly valid snippet of C code that will print out 't': putchar(3["test"]);

I understand why that example works but I struggle finding a valid use-case, aside from code golfing...

Re: First C compiler ported to GCC

#43

Earlier quoted context omitted.

> why should it? It's extremely poor style, even if the behaviour is identical.

So you expect to compiler to give you style points for your code? To be clear: it isn't just coincidentally identical behaviour, it is defined by the standard to be equivalent.

It should at least emit a humorous message asking the developer what are they really trying to accomplish by that.

Re: First C compiler ported to GCC

#44

Earlier quoted context omitted.

> why should it? It's extremely poor style, even if the behaviour is identical.

So you expect to compiler to give you style points for your code? To be clear: it isn't just coincidentally identical behaviour, it is defined by the standard to be equivalent.

Yes, compilers should warn on indisputably poor style, even when program behaviour might still be correct. This is helpful to the programmer, who probably didn't intend to write their code that way.

Fortunately compilers already do this. GCC will warn you about unused variables, for instance.

Re: First C compiler ported to GCC

#45
post #9

Earlier quoted context omitted.

One of the unusual things in this early version of C is that "int" can be used for any word-sized value, including pointers. The type system was very loose.

Even back then this was considered poor practice, however. The first edition of K&R had a subsection entitled "Pointers are Not Integers" (I don't know if that's still in modern editions).

Now I want a float to access individual bits.

Re: First C compiler ported to GCC

#46
post #21

The a[b] implemented as *(a+b) Thing, is how we were taught to think about array indexing in the CS lectures of the 70s

That's how we were taught a few years ago too! It really helped it "click" that array elements are stored contiguously.

Re: First C compiler ported to GCC

#47

Earlier quoted context omitted.

> why should it? It's extremely poor style, even if the behaviour is identical.

So you expect to compiler to give you style points for your code? To be clear: it isn't just coincidentally identical behaviour, it is defined by the standard to be equivalent.

Most compilers will warn about misleading indentation. This is misleading indexing. A program containing misleading indentation is also standards-compliant, but that's completely irrelevant when talking about what code should trigger warnings.

Re: First C compiler ported to GCC

#48
post #21

The a[b] implemented as *(a+b) Thing, is how we were taught to think about array indexing in the CS lectures of the 70s

And that's how it's still taught nowadays. Both the C89 and the C99 standard draft contain the following: > The definition of the subscript operator [] is that E1[E2] is identical to (*((E1)+(E2))) In fact the expressions a[b] *(a + b) and b[a] are equivalent. Here is a perfectly valid snippet of C code that will print out 't': putchar(3["test"]);

How does this work in C++ with operator overloading. Are they still the same? That would make for some interesting obfuscated code.

Re: First C compiler ported to GCC

#49

Earlier quoted context omitted.

Hence why it "can be written" as b[a] as well Edit: it doesn't blow up, not even with -Wall and -std=c99

> (yes it will probably blow up in modern compilers, or at least give you a warning) Nope. For the code snippet I posted an hour ago, even with -pedantic -Wall -Wextra gcc won't issue any warnings. And why should it? It's perfectly standards conformant, because the standard actually defines the [] operator through the equivalent addition expression.

I think the reason the behavior is still there because it is not used. There is no gain in changing the standard, and the compiler warning could draw criticism. Why waste your time solving a non problem?

Re: First C compiler ported to GCC

#50
post #47

Earlier quoted context omitted.

So you expect to compiler to give you style points for your code? To be clear: it isn't just coincidentally identical behaviour, it is defined by the standard to be equivalent.

Most compilers will warn about misleading indentation. This is misleading indexing. A program containing misleading indentation is also standards-compliant, but that's completely irrelevant when talking about what code should trigger warnings.

Then again, it's not common that people make the mistake of confusing an array with an index. Misleading indentation is a somewhat common issue. So it makes sense to have the latter as a warning, but probably nobody thought of adding a warning for the former, or just decided to not bother coding it up.
Post reply on HN