9 ms·
Teaching C (2016)
- dfox 3y agoIn my opinion the implementation of glibc (there is a bunch of good reasons for the “weirdness” and a whole lot of gcc-isms) and even the linked example from musl (with somewhat confusing #ifdef block) is a thing that should be in some kind of “System Programming” course, not in introductory course to C.
- nicklecompte 3y agoThe point is that the course is not introductory: > I’ve always taught C as a side effect of teaching operating systems, embedded systems, or something along those lines....One might argue that we shouldn’t be teaching C any longer, and I would certainly agree that C is probably a poor first or second language. So this would be an upper-level class: an introduction to C programming for people with Python/Haskell/etc experience and a decent general understanding of computer science.
- fsckboy 3y ago[flagged]
- saagarjha 3y agoIt’s a poor worker who blames their tools, but when the tools fail for every worker they kind of suck.
- temporarara 3y agoI don't know. C has worked perfectly fine for me for various tasks and projects over the years. And I'm not alone so there's that, you and your hyperbole are wrong here. Granted I probably wouldn't choose C if safety is one of the biggest issues and it's not a small project, but the blame C gets for just being an honest language which indeed gives you power and the responsibility that comes with it is usually just laughable. C also gives you simplicity which I find delightful. I guess some big projects can indeed profit on complex features and build systems but for smaller stuff it's so easy to just write a simple C program to do it. You really should be using a tool correctly and for the correct use cases, otherwise the critique is meaningless at best. The fact that the 100% sensible and nonoffensive parent comment is now "flagged" just because folks don't agree with it is just ridiculous.
- tialaramex 3y agoThis has a bunch of "But what could possibly replace C?" type comments and the author (writing in 2016) replies Rust. So that's somebody with their eyes on the ball. They also point out that C is a bad first language. This shouldn't be relevant to many Computer Science courses, but I have seen too many Electronics students who are taught C first, or indeed as their only language.
- deleted 3y ago[deleted]
- practicemaths 3y agoWhat's wrong with C? I'm teaching myself with Ritchie at the moment. Seems fine so far but I'm also more interested in embedded systems.
- jimbob45 3y agoC++ is usually better because you can start with the abstractions (e.g. new, classes, I/O streams, and strings) and then work each one backwards once they’re comfortable at the high level. (Although C++ wouldn’t be my first choice to begin with for a first language)
- jll29 3y agoAgreed, C++ is a language riddled with accidental complexity, which hinders the learning process. The fact that its convoluted and ever-changing syntax leads to error messages that are indecipherable even for computer science experts alone makes it a poor choice for learners. Someone who knows nothing would benefit from Pascal a lot even in 2023, even if the practical relevance of the language nowadays is nil (not due to lack of merit, but due to the social dynamics, as a commenter on the OP's original blog righly said) - by the way, R.I.P. Niklaus Wirth. I guess it would be a bit like studying Latin or classical Greek to learn about grammar. Python is "conceptually worse" than Scheme and Pascal - for learners at an academic level, IMHO - but of course practically more useful/valuable, from an industry point of view.
- 3y ago
- deleted 3y ago[deleted]
- alilleybrinker 3y agoIt’s not mentioned in the article, but I also think that learning how to debug and observe the behavior of C programs is useful given the limited ability to catch issues at compile time and high likelihood of code weaknesses and errors in C. Learning not just a traditional debugger like gdb or lldb, but also at least being introduced to tools like perf, strace, and ideally more powerful things like dtrace or bpftrace would be very helpful. In my own Masters program we had a class on software systems which heavily featured both practical coverage of C and lots about debugging and observability. One of the most aggressively _useful_ classes I’ve taken.
- clipsy 3y agoThis sounds like a class that was sorely missing in my CS education; looking back at what I was taught versus what skills have been useful in the industry, my university courses always seemed to treat debugging, profiling, etc as irrelevant details. Taking time to improve my understanding of them has paid off quite well in my career, I think.
- pjmlp 3y agoYes, the poor way this is taught is one of the reasons why so many people are stuck in the stone age of printf debugging. Already in the mid-1990s it was possible to use C debuggers as poor man's REPL, have tracepoints, scripting debugging sessions, visualize data structures, track down memory leaks and corruption.
- einpoklum 3y agoYears ago, I was a teaching assistant at my alma mater's introductory programming course, which taught C. While debugging was not officially part of the curriculum, we made sure to offer a written, step-by-step tutorial with screenshots for people on how to do debugging using our officially-suggested development environment (which was Borland C by the way). We would explicitly mention the importance of debugging when introducing the first homework assignment, and refer students to the tutorial.
- chrsw 3y agoA few good C books have come out in the time since this article was first published, 2016. https://nostarch.com/Effective_C https://nostarch.com/Effective_C https://www.packtpub.com/free-ebook/extreme-c/9781789343625 https://www.packtpub.com/free-ebook/extreme-c/9781789343625 https://www.oreilly.com/library/view/fluent-c/9781492097273 https://www.oreilly.com/library/view/fluent-c/9781492097273
- johnisgood 3y agoI have found https://www.manning.com/books/modern-c https://www.manning.com/books/modern-c really useful. It is written by Jens Gustedt (https://icps.icube.unistra.fr/index.php/Jens_Gustedt https://icps.icube.unistra.fr/index.php/Jens_Gustedt)
- chrsw 3y agoLooks like there's a new edition in the works to cover C23. That could be useful indeed.
- nickpsecurity 3y agoI wrote a lot of criticism of C here. I do want to say the K&R book was a great read when I finally did some hands-on work with C. Credit to Victor Yodaiken for recommending it. The explanations were well-done. They gradually build up the examples. They actually did warn about common gotchas even then. Their examples mostly worked today with only minor tweaks. I used it with Clang’s and MS Visual Studio’s static analyzers.
- TrackerFF 3y agoWhen I studied, C was the first language we learned. I think it was a great way, as we'd learn about memory management, how to do things from "scratch", and since there weren't many batteries included, we kind of just implemented data structures and algorithms as we went on. It also primed us for more involved classes, like operating systems, compilers, databases, and so on. But I have to say, it is a language that students really need to fundamentally know if it is going to be used down the road. Sounds obvious, but when I TA'd in more advanced classes (i.e. anything after "introduction to programming"), many of the students that were struggling, were really just struggling with the language - not necessarily the CS concepts. A lot of them were even students that had programming experience prior to enrolling, but had mostly used higher level languages.
- SLWW 3y agoWhen I first learned C I was a teenager coming from x86 assembly, and before that I was only involved with basic scripting. Having the base of how computers worked on the lowest level, as well as familiarizing myself with ROP and how that works. Only then to move onto C, which was a breath of fresh air. I will say that learning Assembly so early on was rough however it really helped with C. I followed the same path that a lot of earlier men and women went through. A lot of individuals back in the day learned a variation of BASIC, then they moved onto Assembly or C (often both) when things were just too slow for their liking. Going through college I saw exactly what you are saying when it comes to struggling with the language. However most of the individuals I ended up assisting on top of my studying, one-on-one tutoring (if you will); the most glaring thing I noticed was not just a lack of understanding with how the language worked, but more importantly to our discussions, they didn't seem to understand how the fundamentals of computing. How memory is addressed, what exactly are pointers, how de-referencing works in actuality, heap vs. stack, passing to functions to returning values, etc. Once I would walk them through the basics it seemed that they started to understand that while they weren't able to really work effectively within the lower-levels, they would eventually begin to intuit things for themselves much more easily. I don't believe that all that is required to learn how to write code, however if you know how a machine works, you tend to be able to figure out how to treat it or use it. Like with a manual transmission, if you watch visually how a clutch engages, you'll tend to not burn it as much while letting it do it's job, since you know the fundamentals of how it works. It's no longer the "abstract" or "too complicated to understand". Giving people that knowledge gives them the ability and the confidence to delve deeper and genuinely engage with new ways of doing the same thing they've been doing before.
- puzzledobserver 3y agoWouldn't a better first question be _why_ we're teaching students C? One answer might be that although students might spend most of their later professional lives in higher-level languages, they should have some experience in a low-level language. The ideal such language would not have memory management (i.e., not Java / Python), no memory management help from the type system (so no Rust), and no fancy facilities to build abstractions (so no Rust or C++). This language should, ideally, still be a little more pleasant to work with than assembly (so, no assembly). For historical reasons, we would still like unchecked array accesses and null-terminated strings. Now C is an answer to this question, but it need not be the only answer. We could instead teach students a fictionalized version of real C. So nice 2-s complement behavior on overflows, buffer overflows segfaults and other bad behavior, but mostly of the predictable kind. We could define undefined behaviors as the instructor sees fit. As much as we can, we can try to make this fictionalized language agree with the semantics of gcc -O0. We can then study gcc -O0 -S itself as an empirical artifact, and use it to understand x86/x86-64/ARM/MIPS assembly. Finally, while we're at it, we can use the Bourbaki dangerous bend symbol (https://en.wikipedia.org/wiki/Bourbaki_dangerous_bend_symbol https://en.wikipedia.org/wiki/Bourbaki_dangerous_bend_symbol) to repeatedly remind students that what they're studying isn't a real language, that real C has sharp edges (including undefined behaviors), and that things like ASAN / Valgrind / ... exist. I worry that this article is confusing that we'd like students to eventually understand (some understanding of C and an appreciation of the fact that it is an important language which still needs to be handled with care) with the didactic process of reaching that understanding. Wittgenstein's ladder is a thing.
- yjftsjthsd-h 3y agoIf we went to the trouble of making a safe version of C - and we would have to actually implement it; you can't very well teach students to program without letting them actually compile the code and run it - I'd rather just... use that. Call it C24, get it into GCC and Clang, and standardize on something with less Undefined Behavior and painful sharp edges.
- deleted 3y ago[deleted]
- 3y ago
- 6R1M0R4CL3 3y agowe should all learn C and in time, go help that kernel project some of you might have heard of...
- deleted 3y ago[deleted]
- ptek 3y agoAgreed, I'd like to be skilled enough to contribute and add some more drivers to NetBSD or NextStep 3.3/OpenStep 4.2/Rhapsody DR2 (Objective C)
- keyle 3y agoI love C. It's the language that makes me the most happy. I'm not entirely sure why, I think I like being in control. People always comment about C is unsafe, full of razors and knives flying around. The thing to keep in mind is that *you* are the one throwing the razors and knives from the moment you go beyond printf("hello, world!\n"); I'd never recommend a company to start building anything in C. In term of the various team sizes and staff churn over the lifetime of a company, it's too risky. But afaik most companies, particularly the growth hacking ones, are either Go or Kotlin or at that level of abstraction. Rust is slowly eating at C++ and I'm pretty sure no company in their right mind today start a new product in C++. It will slowly become COBOL. C on the other hand still has new C written every day. I'm a polyglot and I love C, but I never had a job in C. I hope some day that happens. The modern tools aforementioned, e.g. static analysis, clangd, are really good. The thing I love most about C is that there is a direct relationship between me doing something stupid and feeling pain for it. In python, I can write crap all day without feeling it. Some day I'll feel it, but not today! I like small binaries and fast code. I like that when a developer tells me I did something wrong, they can prove it, and it's not some stupid high level language "the more you knoooow". I like control over things. I love modern C and I wish it well!
- epcoa 3y ago> I'm pretty sure no company in their right mind today start a new product in C++. The library support for rust in key areas is terribly lacking and not everyone is a Microsoft that can afford to build the world from scratch. As just one example a few valiant folks develop plugins and realtime audio with rust, but serious work that needs to get done today happens in C++.
- 3a2d29 3y ago> I'm pretty sure no company in their right mind today start a new product in C++ I say this as someone who loves rust and zig, but these types of statements make me feel like HN is way out of touch with the industry. New products are constantly made in C++. That doesn't make it good (and i don't think companies building in C++ are making the best choices), but to say that no big tech company is writing any new code in C++ is just not correct. I see this happen every day at my job. I also would definitely say more C++ code is written every day over C, although I'll say, I am not as familiar with the embedded world, but I know one HFT/Gaming/Robotics firm will have millions of new C++ lines every day.
- KingLancelot 3y ago[dead]
- WalterBright 3y agoI was an experienced asm programmer before a friend loaned me a copy of K+R C. I understood C immediately. I imagine trying to learn C without knowing assembler would be a much tougher road. Knowing C also helps in understanding a lot of other languages.
- deleted 3y ago[deleted]
- ykonstant 3y agoYour comment prompted me to submit the following question at reddit: https://www.reddit.com/r/learnprogramming/comments/18zz72t/draw_stickman_in_linux_tty_console_with_assembly/ https://www.reddit.com/r/learnprogramming/comments/18zz72t/d...? If you know of a better place to ask such questions, let me know.
- pjmlp 3y agoSame here although coming from multiple languages, so C was only another syntax to do the same things I was already doing in those languages. As such I never understood what is so hard about pointers, we were already close friends before learning C.
- WalterBright 3y ago> pointers The first thing learned with assembler is pointers and addresses. So I don't know how hard it would be to get them coming from other languages. But I did first learn to program in Basic and Fortran, which gave zero insight whatsoever when subsequently learning assembler. I was pretty baffled by it for a while, until I suddenly "got it".
- pjmlp 3y agoIt helped that on 8 bit home computers BASIC was the OS and systems language, exposing the whole hardware, including having pointer like mechanisms, via PEEK, POKE, DATA, USR, PTR, among others. Diving into Z80 was a matter of performance, not additional features.
- dang 3y agoRelated: Teaching C (2016) - https://news.ycombinator.com/item?id=32798826 https://news.ycombinator.com/item?id=32798826 - Sept 2022 (94 comments) Teaching C (2016) - https://news.ycombinator.com/item?id=18334476 https://news.ycombinator.com/item?id=18334476 - Oct 2018 (211 comments) Teaching C - https://news.ycombinator.com/item?id=11668396 https://news.ycombinator.com/item?id=11668396 - May 2016 (152 comments)
- anta40 3y ago>> adding some high-quality C code, perhaps starting with Redis, Musl, or Xv6. Agree. Xv6 is the reason why I used C. I learnt more about various OS concepts (process scheduling, file system etc) by hacking Xv6 code. Linux is too complicated for my taste. As a mobile app dev, I don't use C. Well.... I do. Very ocasionally though. Only for interacting with specific Android hardwares.
- beyonddream 3y agoI have been programming in C professionally for the past ~4 years on a multi-threaded application in the ads industry and that is central to the business. C has lot of documented "foot-guns" but most of it (if not all of it) can be safeguarded against mis-use using "support structures" around the codebase like having standard code style (enforced via tools as much as possible), static code analysis (via compiler and external code scanning tools), dynamic code analysis (via Asan, Ubsan etc), having small, medium and large tests with right amount of code coverage etc. In addition to that, having a standard set of libraries (c modules for high performance data-structures/algorithms, macro based templates to work with types) and threading model goes a long way in reducing the pain to a very bare minimum. On the plus side, you enjoy a "simpler" language syntax that is easy to learn and with the above “support structures” one can become productive in no time. Plus there are newer books in the market that can teach you C properly, one I have read and recommend is "Effective C by Robert C. Seacord" - the author is one of the C standards committee members so you can't go wrong with the choice.
- deleted 3y ago[deleted]
- countWSS 3y agoMany people complain about teaching C leading to bad habits or something of that sort. C is crucial to understanding OS apis, driver code and low-level algorithms, its not locked to single platform like assembly and allows composing fairly high-level abstractions without any hidden overhead. Writing your own libc functions and understanding how they work is essential to systems programming.
- lifthrasiir 3y agoThe complaint is probably more about teaching C too early and getting fixated on that. The importance and eventual need for learning C is hardly questioned.
- pjmlp 3y agoThe problem is not teaching C, for better or worse it is going to stay around us for a very long time, thanks to UNIX's victory on server room and embedded systems. The actual problem is doing a half baked job, teaching it as if PDP-11 were still current, without any consideration for safe programming practices and modern tooling.
- sys_64738 3y agoThese bad habits you mention should be taken care of by general programming concepts classes. If you're using a new language like C for OS development and you are thinking of goto and other frowned on methods then a person really didn't do the programming concepts class very well.
- prudentpomelo 3y ago>> There’s a lot of reading material out there. For the basics, I still recommend that students purchase K&R. Has anyone read K&R and regretted it? I am working through all the exercises and have really enjoyed the process.
- heads 3y agoThe thing that really got me into this subject was building my own virtual machine and then a compiler to turn a high level language into machine code. It sounds complicated but the VM, in Python, is basically: r = {} mem = [] boot(mem) pc = 0 while True: op, args = decode(mem[pc++]) if op == “+”: d, a, b = args r[d] = r[a] + b elif ...: ... The “source” which gets “compiled” can also just be Python function calls that emit op codes — no need to get bogged down with lexing and parsing! The thing you focus on is the semantics of representing a function call (and return) in machine code and how to manage the stack. It made C make a lot more sense e.g. defining locals up front so the compiler knows how much stack space to use. I wish I could remember more — this was all from teaching A-Level Computer Science a few years ago.
- samsquire 3y agoAgreed. I find C easier to understand than the algebra of other languages such as Haskell, Ocaml, Rust because of how C statements correspond to assembly. Memory is just a big grid of numbered locations and computing is logistics between grid locations. Execution is transition between memory locations. LEAQ or "&" (C addressof) is an address calculation or LEAQ for one of those grid locations.
- IshKebab 3y ago> C statements correspond to assembly True in the 80s maybe. Not remotely true now.
- samsquire 3y agoI don't mean that there is a 1-1 constant relation between C statements and exact assembly, but if you run gcc with -S or run objdump -b binary -Matt,x86-64 -D main.bin -m i386 ; (note: this command places the assembly and C code side next to eachother, I think you have to compile with -g debug flag with gcc) You can work that the assembly implements the semantics of C's virtual machine model and it is this that I find easier to understand than some algebraic systems. I kind of think of struct->ptrarray[indx]->struct.something corresponds to the calculation of ultimately a single memory address even if there is various shift lefts or adds or ors. Java has its template interpreter which is interesting to read about and there is copy and patch JIT compilers.
- sgbeal 3y ago> A stronger tool such as the Clang static analyzer should also be used. While also teaching students that static code analyzers are very far from infallible and can waste a great deal of developer time with false positives. It happens about once every month or two that someone posts on the sqlite forum, "I've found this serious bug..." and, after some back and forth, it's revealed that they used a static analyzer or, even worse, just pasted a single internal function from the sqlite core into ChatGPT and asked it to analyze the code for them (completely free of any calling context). Roughly 9 times out of 10, static code analyzers are flat out wrong in their analysis of that particular source tree. It's very rare that a report in sqlite which stems from a static code analyzer is actually correct. The advice given to such posters is invariably: if you can demonstrate code which tickles the bug your tool is claiming to have found, it will be treated with priority. Until then... not so much.
- ahoka 3y agoCoverity used to suggest using strncpy instead of strcpy for a while, which if you blindly follow leads to nasty errors. If someone wonders, snprintf was the correct alternative at the time, I have no idea if the C standard has anything better now.
- DrNosferatu 3y agoNo mention of the Deitel & Deitel books?
- toasted-subs 3y agoI feel like very rarely do u feel as satisfied as when I program in C.
- anthk 3y agoIf you are a seasoned C programmer, helping GNU Hurd, for instance, with the Rump kernel addon to add NetBSD driver support (please, no blobs) would be a good thing to have. https://www.gnu.org/software/hurd/ https://www.gnu.org/software/hurd/ You can debug drivers on the fly with gdb, something much more difficult to do under OpenBSD, NetBSD and Linux-(libre). Also, you can run Hurd inside Hurd, a la chroot. But no root permissions are needed. https://www.gnu.org/software/hurd/community/gsoc/project_ideas.html https://www.gnu.org/software/hurd/community/gsoc/project_ide... Yep, C is the Unix and Unix-like ABI, and an odd form of C it's under 9front/plan9's ABI/API too, but this makes the userland much more powerful than the typical permission-isolated Unix system where the user has near no capabilities even for mounting non-external media (by default).
- flohofwoe 3y agoOne thing I would try to do (in addition to the things mentioned in the article) is to delay the introduction of heap allocation and pointers as much as possible, and start with a purely "value based" code style which passes and returns structs by value, especially use C99 features like compound literals and designated initialization. The result is a much saner and 'friendlier' C. Also ignore the stdlib as much as possible, especially the string functions. The C stdlib APIs are essentially ancient leftovers from the K&R and early C89 era and should have been modernized when C99 came around. Especially for newbies, paying too much attention to the stdlib may ingrain bad habits.
- mi_lk 3y agowhat should be the replacement for C stdlib?
- hgs3 3y agoThe stdlib is mostly fine. The string and character classification functions are outdated because they aren't Unicode aware. There's a more detailed write up here [1]. [1] https://nullprogram.com/blog/2023/02/11/ https://nullprogram.com/blog/2023/02/11/
- flohofwoe 3y agoIt's not just the missing UNICODE support (although that's probably out of scope for the stdlib, a couple of UTF-8 aware helper functions would be good enough), but footguns-in-waiting like strcpy(), strncpy(), strcat(), strncat(), ... All the wide string stuff (wchar.h and wctype.h) as well as locale.h is all pointless today. Everything related to filesystems and file IO is just the bare minimum that's acceptable for simple UNIX-style command line tools, but not really useful in modern applications (most notably, any support for non-blocking IO and memory-mapped-files are missing). Arguably, complex.h doesn't even belong in the stdlib (I wonder why such an esoteric feature was even considered). Better support for custom allocation strategies would be nice (e.g. each function which allocates under the hood should accept an allocator argument instead of being hardwired to malloc). I'm not even asking for a more feature-rich stdlib, a stdlib2 with the obsolete parts removed and modernized IO and memory management functions would go pretty far. But then of course there's the problem that the C stdlib is also accidentially the de-facto system API on some UNIXes.
- jll29 3y agoAfter programming already for many years, the book "C: Interfaces and Implementations" by Hanson still showed me some new tricks for writing libraries in a more bullet-proof way, and I have mentioned or used it in every C course I have taught ever since. The book contains some production-quality data type implementations that aspiring C programmers ought to read.
- sys_64738 3y agoTheir approach is wrong. You don't say at a university level that you're going to teach C in a class. That's not what university level courses are about. If you're teaching programming applications and techniques then use a relevant language which supports those concepts. Learning a language should be a bi-product of the university course, not the fundamental reason for it. You learn C as part of any myriad of courses such as networking, but then it is simply a lecture or two then students need to get up to speed themselves.
- InvOfSmallC 3y agoI'm a contract professor at uni. I think that he should put which kind of students you are teaching C to. I am their first experience at university and for some of them C us their first programming experience. In this case my only advice is just keep repeating the same subject in more than one lesson. What is suggested into the article seems to me more about "how do I run C in production". But good suggestions anyway, I'll review them to see what I could use if it makes sense maybe when we do lab.