News for hackers. Stuff that matters.
Re:The Ten Commandments for C Programmers (1987) by microtherion on Saturday May 23, 2020 @11:51AM (#23283915)
This should probably carry a date stamp; my guess would be that it dates from 1990 or so.

Most of the advice is still good, but I would object to (3):

* Excessive casting can be a problem in itself, as it hides genuine type errors (e.g. you think you're casting an int to long, but in reality the variable is a pointer)

* Casting NULL is pointless, as it's already defined by the standard to be a pointer constant, compatible with all data pointers.

* Much of the language about "prototypes" (a.k.a. standard C) was justified at the time, but no longer reflects reality nowadays. It would have to be a very exotic platform that still uses a pre-ANSI C compiler.

Re:The Ten Commandments for C Programmers (1987) by CliffStoll on Saturday May 23, 2020 @12:15PM (#23284133)
Dated? #10 is from a 1980's MOTD message of the day: All the world's a VAX, And all the coders merely butchers; They have their exits and their entrails; And one int in his time plays many widths, His sizeof being N bytes. At first the infant, Mewling and puking in the Regent's arms. And then the whining schoolboy, with his Sun, And shining morning face, creeping like slug Unwillingly to school. -- A Very Annoyed PDP-11
Re:The Ten Commandments for C Programmers (1987) by microtherion on Saturday May 23, 2020 @01:31PM (#23284770)
Heh. Getting a response from you must be peak 1990 ;-)

By the time I got started on Usenet "All the world's a VAX" was only in use as a proverbial misconception anymore. At the time, all the world was a Sun Workstation.

Re:The Ten Commandments for C Programmers (1987) by neilv on Saturday May 23, 2020 @02:53PM (#23285411)
Yep, circa 1990, a SPARCstation or Sun-3 running SunOS 4 seemed the place to be, for having most software off the 'net build easily.

I also worked on almost all the other engineering workstation platforms, for porting purposes, but a SPARCstation was my preferred main workstation, overall.

(I was a kid who stumbled upon a dream job. So many expensive toys, so much I could learn, and this cool Internet thing...)

Re:The Ten Commandments for C Programmers (1987) by icedchai on Saturday May 23, 2020 @10:05PM (#23288463)
I bought a Sparc 5 back in 1996 or so, from a company going out of business. I felt like a king. Nobody had stuff like that at home back then.
Re:The Ten Commandments for C Programmers (1987) by jacobush on Saturday May 23, 2020 @03:40PM (#23285740)
Wow... just seeing a post of Cliff Stoll is incredible. I love that TV movie and the book.
Re:The Ten Commandments for C Programmers (1987) by eMSF on Saturday May 23, 2020 @12:40PM (#23284344)
>* Casting NULL is pointless, as it's already defined by the standard to be a pointer constant, compatible with all data pointers.

You should still cast NULL when passed as an unnamed argument to a variadic function. While NULL is defined to be a null pointer constant, that doesn't mean it has a pointer type (all integer literals with a value of zero are null pointer constants in C, but they have an integer type).

(Strictly speaking, all unnamed pointer arguments to variadic functions should be converted to a compatible pointer type that the variadic function expects. For example, the printf-line of functions expect a pointer-to-void argument for a conversion specifier %p. If you pass a pointer-to-int instead, technically the behaviour is undefined.)

(To whom it may concern: I would appreciate at least a rebuttal. It's not like this is a subjective matter.)

Re:The Ten Commandments for C Programmers (1987) by monocasa on Saturday May 23, 2020 @01:39PM (#23284829)
It's unfortunate that you're getting downvoted; there's for sure real dragons at the intersection of the NULL literal and variadic functions since it's totally legal for NULL to be defined as such:

  #define NULL 0
Re:The Ten Commandments for C Programmers (1987) by a1369209993 on Wednesday May 27, 2020 @10:37PM (#23332809)
Much, much more subtly nasty, it's also legal for NULL to be defined as:

  #define NULL ((void*)0)
and then passed to a function that expects variadic int* parameters on a machine where register-sized addresses are word-aligned and thus int* has a different sizeof (and/or bit-level representation) than void*.
Re:The Ten Commandments for C Programmers (1987) by microtherion on Saturday May 23, 2020 @01:51PM (#23284930)
You're right, I was misinterpreting the language of the standard.

As a practical matter, C implementations typically define NULL as a void pointer, so the cast is not necessary in those implementations (In C++ pre C++11, NULL is always defined as a literal 0, so the cast is definitely necessary).

But it appears the standard allows C implementations to define NULL as a literal 0 or as 0L as well, so the cast is needed in portable code.

Re:The Ten Commandments for C Programmers (1987) by verall on Sunday May 24, 2020 @12:05PM (#23292440)
> If you pass a pointer-to-int instead, technically the behaviour is undefined.

But if you cast the intptr to voidptr, the behavior is defined, right? (Besides that %p is nonstandard iirc)

Re:The Ten Commandments for C Programmers (1987) by eMSF on Sunday May 24, 2020 @03:52PM (#23294191)
Yes. Also, %p isn't non-standard, however the conversion is implementation-defined.

(The reason why I used the word technically earlier was because on all common platforms int-pointers and void-pointers – and indeed all object pointers – have identical binary representation.)

Re:The Ten Commandments for C Programmers (1987) by kps on Saturday May 23, 2020 @12:15PM (#23284134)

    Subject: Ten Commandments For C Programmers
    Message-ID: <[email protected]>
    Date: Wed, 14-Oct-87 17:35:26 EDT

    Subject: The Ten Commandments for C Programmers (Annotated Edition)
    Message-ID: <[email protected]>
    Date: 4 Nov 90 02:16:18 GMT
Re:The Ten Commandments for C Programmers (1987) by neilv on Saturday May 23, 2020 @02:58PM (#23285450)
1990, University of Toronto Zoology... one could make a make a good guess at the poster: https://en.wikipedia.org/wiki/Henry_Spencer :)
Re:The Ten Commandments for C Programmers (1987) by earleybird on Saturday May 23, 2020 @03:25PM (#23285639)
Re:The Ten Commandments for C Programmers (1987) by ajross on Saturday May 23, 2020 @12:16PM (#23284139)
> It would have to be a very exotic platform that still uses a pre-ANSI C compiler.

Not "exotic" so much, really. Obviously those days are long behind us now, but for many years the very last refuge of K&R C was the bootstrap compiler for gcc.

By the 90's, for revenue and product differentiation reasons all the big Unix vendors had moved their optimizing toolchains into their "developer" software packages and out of the base system. But for legacy compatibility reasons they still needed a working K&R C compiler in /bin/cc.

So for almost two decades, the only contact working engineers had with pre-ANSI compilers was using them to bootstrap a real compiler (gcc) on their new, fancy, high performance, very mainstream and non-exotic workstations.

Re:The Ten Commandments for C Programmers (1987) by enriquto on Saturday May 23, 2020 @12:10PM (#23284083)
This! Casting the output of a function that returns (void*) is usually the first red flag of code not written by an experienced C programmer.
Re:The Ten Commandments for C Programmers (1987) by rrss on Saturday May 23, 2020 @12:18PM (#23284156)
it can also just indicate that when it was written it also had to compile as C++ for whatever reason.
Re:The Ten Commandments for C Programmers (1987) by enriquto on Saturday May 23, 2020 @02:07PM (#23285027)
That's an even redder flat. Why would you even want or need that? I understand that sometimes it is convenient to write header files that can be read likewise by C and C++ compilers. But why would you ever need a piece of code to be compiled into an object by either C or C++ compiler, indistinctly?
Re:The Ten Commandments for C Programmers (1987) by flohofwoe on Saturday May 23, 2020 @02:32PM (#23285247)
That was essentially Microsoft's advice because they didn't want to keep their C compiler uptodate:

"We recommend that C developers use the C++ compiler to compile C code (using /TP if the file is named something.c). This is the best choice for using Visual C++ to compile C code."

From: https://herbsutter.com/2012/05/03/reader-qa-what-about-vc-an...

Re:The Ten Commandments for C Programmers (1987) by enriquto on Saturday May 23, 2020 @02:39PM (#23285303)
I'm going to abstain from commenting on this topic. I am not emotionally prepared.
Re:The Ten Commandments for C Programmers (1987) by rrss on Saturday May 23, 2020 @03:32PM (#23285692)
There are several reasons why people would do this, but the decision is often not made by the person writing the code, so it may be a red flag to you, but it doesn't point to inexperience of the author.

In addition to the MSVC issue mentioned in the sibling comment, I've worked on projects that used clang thread safety analysis, which at the time (and maybe still today) was only available for C++, so the build for that analysis was configured to build all C source files as C++.

Re:The Ten Commandments for C Programmers (1987) by throwphoton on Saturday May 23, 2020 @12:15PM (#23284125)
What can you do with a void pointer without casting it, apart from comparing it to NULL?
Re:The Ten Commandments for C Programmers (1987) by eMSF on Saturday May 23, 2020 @12:21PM (#23284180)
You can (implicitly) convert it to another pointer type, e.g.

    int *buf = malloc(sizeof(*buf));
(It's noteworthy that while there's an implicit conversion above, there's no such thing as an implicit cast.)
Re:The Ten Commandments for C Programmers (1987) by unwind on Saturday May 23, 2020 @01:44PM (#23284861)
This is a pretty well-regarded answer, with motivation for why you should not cast [1].

Of course it's not something everyone agrees to, but at least a lot of people seem to.

[1] https://stackoverflow.com/questions/605845/do-i-cast-the-res...

Re:The Ten Commandments for C Programmers (1987) by throwphoton on Saturday May 23, 2020 @07:31PM (#23287595)
Interesting. Not casting a void pointer return value seems like a bit of a shibboleth for the "C is not a subset of C++" community.
Re:The Ten Commandments for C Programmers (1987) by enriquto on Sunday May 24, 2020 @10:26AM (#23291742)
A "better" shibboleth is using "new" and "old" as identifiers. It serves as a good barrier to avoid accidental C++ compilation.
Re:The Ten Commandments for C Programmers (1987) by glouwbug on Sunday May 24, 2020 @12:50AM (#23289191)
Generic data structures work great if they use a void* since malloc returns a void*. Just malloc your structs, and suddenly a single set of 10 or so functions can work with all data types to build queues, lists, hash maps, etc
Re:The Ten Commandments for C Programmers (1987) by _kst_ on Saturday May 23, 2020 @04:34PM (#23286161)
You can compare it for (in)equality to another void pointer.
Re:The Ten Commandments for C Programmers (1987) by microtherion on Saturday May 23, 2020 @01:35PM (#23284800)
You can assign it to any data pointer variable, and you can printf("%p") it, for example.
Re:The Ten Commandments for C Programmers (1987) by adrianN on Saturday May 23, 2020 @12:15PM (#23284127)
I dislike implicit type conversions, but I think every cast needs to be accompanied by an assertion. Just sprinkling casts everywhere does nothing but silence important compiler warnings.
Re:The Ten Commandments for C Programmers (1987) by phkahler on Saturday May 23, 2020 @01:54PM (#23284950)
>> I dislike implicit type conversions...

What do you think of "auto" in C++ ?

Re:The Ten Commandments for C Programmers (1987) by codys on Saturday May 23, 2020 @06:13PM (#23287013)
Not the parent, but `auto` in c++ improves the situation wrt implicit conversions as variables now automatically get the type of the thing assigned to them. IOW: less unintentional implicit conversions.
Re:The Ten Commandments for C Programmers (1987) by bl0b on Saturday May 23, 2020 @02:14PM (#23285058)
'auto' is only available to use when the type of a variable can be automatically deduced from its initializer, meaning the declaration would be redundant. It has nothing to do with type conversions.

edit: now I see what you mean in the context of adding assertions to make type conversions safer, and how there could be some parallels with auto for that

Re:The Ten Commandments for C Programmers (1987) by dllthomas on Saturday May 23, 2020 @03:05PM (#23285500)
It's an example of making something explicit implicit.

Whatever the answer, it won't surface a contradiction; but that doesn't mean it's not an interesting, obliquely related question.

Re:The Ten Commandments for C Programmers (1987) by phkahler on Saturday May 23, 2020 @03:25PM (#23285641)
It's implicit declaration, which seems worse in some ways than implicit conversions. After a conversion you know what type you have even if not the type you started with.
Re:The Ten Commandments for C Programmers (1987) by WalterBright on Saturday May 23, 2020 @05:10PM (#23286479)
> prototypes

The following C code compiles today without complaint on gcc:

    foo(i, j) { return i + j; }
Re:The Ten Commandments for C Programmers (1987) by burfog on Sunday May 24, 2020 @01:13AM (#23289294)
An interesting thing about the old system is that it could have checked data types just fine. This would have required linker support, but in the early years C didn't have the influence to make that happen. It would have been the tail wagging the dog. Linkers were designed for other languages, then used by C.

With linker support, C++ would never have had to resort to name mangling.

The foo(i,j) in your example could have caused the compiler to emit assembly directives to specify the argument types and return type. The assembler could have put these into the object file. The linker could have checked for compatibility when linking. The linker could have produced warnings, errors, or in some cases even thunks.

Re:The Ten Commandments for C Programmers (1987) by microtherion on Sunday May 24, 2020 @05:55AM (#23290446)
Yes, I did not mean to imply that old-style code no longer compiles today, but that support for prototypes is near-universal nowadays.
Re:The Ten Commandments for C Programmers (1987) by yakshaving_jgt on Sunday May 24, 2020 @12:09AM (#23289001)
From the following passage, I would have thought this would have been written as recently as today.

> As a lamentable side issue, there has been some unrest from the fanatics of the Pronoun Gestapo over the use of the word ``man'' in this Commandment, for they believe that great efforts and loud shouting devoted to the ritual purification of the language will somehow redound to the benefit of the downtrodden (whose real and grievous woes tendeth to get lost amidst all that thunder and fury). When preaching the gospel to the narrow of mind and short of temper, the word ``creature'' may be substituted as a suitable pseudoBiblical term free of the taint of Political Incorrectness.

Re:The Ten Commandments for C Programmers (1987) by anaphor on Saturday May 23, 2020 @05:49PM (#23286830)
I would add "Never cast malloc" to that as well. It's pointless and only required if you're writing C++, which is not C.