News for hackers. Stuff that matters.
Re:Weekend projects: getting silly with C by JohnMakin on Sunday June 30, 2024 @05:54AM (#40836165)
My undergrad was entirely in the C language and I’m very glad for it. Sometimes more modern languages can throw me for a loop, no pun intended, but the beauty (and horror) of C is that you are pretty close to the metal, it’s not very abstracted at all, and it allows you a lot of freedom (which is why it’s so foot gunny).

I will never love anything as much as I love C, but C development jobs lie in really weird fields I’m not interested in, and I’m fairly certain I am not talented enough. I have seen C wizardry up close that I know I simply cannot do. However, one of the more useful exercises I ever did was implement basic things like a file system, command line utilities like ls/mkdir etc. Sometimes they are surprisingly complex, sometimes no.

After you program in C for a while certain conventions meant to be extra careful kind of bubble up in languages in a way that seems weird to other people. for example I knew a guy that’d auto reject C PR’s if they didn’t use the syntax if (1==x) rather than if (x==1). The former will not compile if you accidentally use variable assignment instead of equality operator (which everyone has done at some point).

This tendency bites me a lot in some programming cultures, people (ime) tend to find this style of programming as overly defensive.

Re:Weekend projects: getting silly with C by frou_dh on Sunday June 30, 2024 @07:05AM (#40836505)
> for example I knew a guy that’d auto reject C PR’s if they didn’t use the syntax if (1==x) rather than if (x==1). The former will not compile if you accidentally use variable assignment instead of equality operator

I've seen that one and personally dislike that mindset: Making the code less readable to compensate for a disinterest in using actual static analysis tooling.

Re:Weekend projects: getting silly with C by tuveson on Sunday June 30, 2024 @11:06AM (#40837570)
These days GCC and Clang will both give you warnings for this if you have -Wall, which everyone should.
Re:Weekend projects: getting silly with C by kubanczyk on Monday July 01, 2024 @06:14PM (#40851379)
Less readable? I agree these are less readable:

  if (987654321987654321==x)

  if (find_optimal(frobnicator(x)*8)==1)
while these are subjectively more readable:

if (x==987654321987654321)

  if (1==find_optimal(frobnicator(x)*8))
Re:Weekend projects: getting silly with C by 8372049 on Sunday June 30, 2024 @07:03AM (#40836494)
> if they didn’t use the syntax if (1==x) rather than if (x==1). The former will not compile if you accidentally use variable assignment instead of equality operator

No need for Yoda notation. clang will warn of this by default and gcc will do so if you compile with -Wall, which should also be your default.

Re:Weekend projects: getting silly with C by uecker on Sunday June 30, 2024 @06:23AM (#40836297)
I force my students to do C development. And it turns out that it is not that hard if you approach it with modern tools which catch a lot of problems. The lack of abstraction is fixed with good libraries.

C evolved a lot and many foot guns are not a problem anymore. For example for

if (x = 1)

you nowaday get a warning. https://godbolt.org/z/79acPPro6

Implicit int, calling functions without prototypes, etc. are hard errors. And so on.

Re:Weekend projects: getting silly with C by tialaramex on Sunday June 30, 2024 @06:55AM (#40836446)
The warning says to add parentheses, which sure enough silences the warning, your foot, however, still has a bullet hole in it.
Re:Weekend projects: getting silly with C by uecker on Sunday June 30, 2024 @07:12AM (#40836531)
The warning is very clear. If you did intend to use the result of an assignment as truth value, you would notice. In any case, did not have a single problem with this type of error in the last decades, working with programmers of various skill levels including beginners.
Re:Weekend projects: getting silly with C by lelanthran on Sunday June 30, 2024 @11:08AM (#40837583)
> The warning says to add parentheses, which sure enough silences the warning, your foot, however, still has a bullet hole in it.

The warning also says that it's an assignment. It's a pretty clear warning meant to force the programmer to do extra work to get the error.

Re:Weekend projects: getting silly with C by Joel_Mckay on Sunday June 30, 2024 @07:13AM (#40836542)
The libglib-dev with gcc is very handy for toy projects, but only _after_ students try to write their own versions:

https://docs.gtk.org/glib/data-structures.html

It could be fun to do a lab summary after the lists and hashes introduction.

Have a wonderful day, =)

Re:Weekend projects: getting silly with C by uecker on Sunday June 30, 2024 @07:20AM (#40836570)
I absolutely I agree that learning to create you own abstractions is an incredible useful skill. It depends though. For a programming course this makes absolutely sense. But for applied problems in, say, biomedical engineering, this does not work. Many students know only a bit of Python, and then it is too much and "too inconvenient" to start from scratch in C. With Python they have a lot of things more easily available, so they make quick progress. This does not lead to good results though! For most of the Python projects, we end of throwing away the code later. Another problem is that students often do not know what they are doing, e.g. the use some statistical package or visualization package and get nicely looking results, but they do not know what it means and often it is entirely wrong. For machine learning projects it is even worse. So much nonsense and errors from copying other people Python code....
Re:Weekend projects: getting silly with C by Joel_Mckay on Sunday June 30, 2024 @08:58AM (#40836912)
Python like Basic abstracted far to many details away from students, and trying to convince people they need to know how a CPU works later is nearly impossible.

In general, digging deep enough down a stack, and it drops back into the gsl:

https://www.gnu.org/software/gsl/

Indeed, first month attrition rates for interns at some companies is over 46%. =3

Re:Weekend projects: getting silly with C by smackeyacky on Sunday June 30, 2024 @05:56AM (#40836173)
In an embedded environment, overly defensive is an asset
Re:Weekend projects: getting silly with C by JohnMakin on Sunday June 30, 2024 @06:05AM (#40836218)
That’s precisely where my little professional C experience was. I then switched to a python shop and was initially horrified at some conventions, took some getting used to.
Re:Weekend projects: getting silly with C by mgerdts on Sunday June 30, 2024 @08:42AM (#40836842)
> I have seen C wizardry up close that I know I simply cannot do.

I have written C at least a few times per year for over 30 years. About ten years of that was OS development on Solaris and its derivatives.

Articles like this show crazy things you can do in C. I’ve never found the need to do things like this and have never seen them in the wild.

The places that wizardry is required are places like integer and buffer overflow, locking, overall structure of large codebases, build infrastructure, algorithms, etc. Many of these are concerns in most languages.

> auto reject C PR’s if they didn’t use the syntax if (1==x) rather than if (x==1)

When I was a student in the 90s advice like this would have been helpful. Compiler warnings and static analyzers are so much better now that tricks like this are not needed.

Re:Weekend projects: getting silly with C by lelanthran on Sunday June 30, 2024 @11:05AM (#40837563)
> I knew a guy that’d auto reject C PR’s if they didn’t use the syntax if (1==x) rather than if (x==1). The former will not compile if you accidentally use variable assignment instead of equality operator (which everyone has done at some point).

That's not so much of a footgun anymore - the common C compilers will warn you about that so there's not much point in defending against it.

Same with literal format string parameters to printf functions: the compiler is very good at warning about mismatched types.