<- Back
Comments (96)
- ueckerAny tutorial for C should explain compiler warnings and other tools that help make code safer. In C, you do not rely on the language specification but on tooling. Especially for Rust programmers, this needs to be explained more explicitly. And of course, you can build abstractions using types in C. So a tutorial should also focus on that, and perhaps not start with a low-level string reversal function.
- bennettnate5Some fun quirks I never learned until years after first getting into C:- zero-initialization via `= {0};` will still leave padding and any data not covered by the smallest elements of unions uninitialized, so serializing data structures zeroed via that method is unsafe- A pointer that increments any more than 1 past the end of a valid memory region (e.g. the end of a buffer) is instant undefined behavior even if the pointer is never dereferenced- strict aliasing is on by default for pointers of differing types, but not for void/char/uchar. This means that having `struct sockaddr` and `struct sockaddr_in` pointers pointing to the same struct is UB.The more I learn, the more I run from C.
- phamiltonRe: arrays are pointers.One of my favorite things to show just how bare this is in C is to show array access commutativity. char c = {1,2,3} c[1] == *(c + 1) *(c + 1) == *(1 + c) c[1] == 1[c] C is wonderfully simple at times.
- rini17My biggest bewilderment with C is that malloc has to store the buffer size otherwise free could not work...but nobody in decades thought to make this information accessible to the programmer! If you want to keep track of buffer bounds in C, you have to do it artisanally. Despite it's, in most implementations, stored right there next to the data and thus in L1 cache already.
- ReDressI'm going to go out on a limb here and make a wild guess.That boolean is actually mostly an extension of the integer system whereby we now have an integer type that stores only one bit.Whereby the bit stored either results in a 'true' or 'false' value.Anyways, I know booleans are useful in systems development when you have strict memory / storage constrains / bandwidth (networks).Yeah, that works for me.
- layer8> Woah, iterating over pointers instead of indexes! […] However, I'm not sure how good an idea that is.I’d recommend anyone (including the author) wanting to understand C to read K&R’s “The C Programming Language”, which among other things will illustrate how iterating over pointers is idiomatic in C (though not quite in the way the author’s example does it).
- bjackmanIt's beautiful to see this perspective! The fact that we are at "whoa, C is fucked up compared to my expectations" instead of "look at Rust's fancy safety stuff" shows that as a field we've started lifting the baseline.Nowadays I actually believe I'll likely see a world without memory corruption within my lifetime.
- kvemkon> the program should check for a null pointer and gracefully exit if one is found: ...If malloc() fails, there is no need to exit the program completely with exit(), only return from the current function with an error.
- _dain_>The very first systems programming language I ever learned was Rust. This is uncommon compared to many other programmers; you're more likely to find someone who learned C or C++ first before coming to Rust.It's becoming increasingly common. Rust is my first systems programming language too. I tried to learn C a few years ago but I found it too austere and prickly, which put me off.
- rramadassRead the following if you want to be a C Programmer;Fluent C: Principles, Practices and Patterns by Christopher Preschern - https://www.oreilly.com/library/view/fluent-c/9781492097273/
- blourvimIt reads really well
- pwdisswordfishqisize and usize seem to be more like intptr_t and uintptr_t. To be fair, for a long time it was not very well-defined to which C type they correspond to; I think that was cleared up only recently.It's also a shame the article uses the self-delusional C++ style of pointer declarators.Otherwise pretty okay.
- jmclnxReally just another use Rust instead of c article.
- elendilmMy own journey has been quite different.When I was building my kernel in my late teens, I first wrote the bootloader by hand on paper in assembly language. I then referenced the x86 manual for the instruction set and converted my assembly code into the equivalent hexadecimal machine code values of the x86 machine instructions, which I also wrote by hand on paper. Then I used a hex editor on the desktop to manually write the hex values into a file and used it as the bootloader i.e as the first 512 bytes.The whole exercise gave me a sense of hard grounded zero magic, raw, unfiltered experience. This is an experience that is hard to replicate in any other way. I deliberately did that so as to peel away as much magic/abstraction layers as I possibly can.Later on when I started using C, I never had to learn C but merely just had to reference the equivalents of the assembly language. Like how the primitive "if" doesn't exist in the hardware but is a composition of cmp and jmp instructions. Seen this way, C becomes a glorified portable syntactic sugar over assembly language.Then higher up the ladder to C++ for object oriented problem solving while retaining the spirit of functional programming. Rust was a breath of fresh air, where correctness across a myriad of use cases was a first class primitive.Each abstraction layer can thus be evaluated for its utility in problem solving while its underlying mechanics remain understandable down to the hardware level.More recently, our own arcc compiler extends correctness to our architecture and not just the types.So if you are young and have time to spare, I suggest a little bit of Assembly => C => C++ => Rust.This essentially makes you immune to hype train bullshit.
- BitProgramThis article is written for Rust programmers learning C. As someone in the opposite position I know C and I've been curious about Rust I'd love to read the reverse. What's the hardest thing for a C programmer to unlearn when moving to Rust? Is it the borrow checker, or is that just the thing people talk about because it's the most visible?
- caaqil> there are plenty of "Rust for C Programmers" articles on the internet, but little to no "C for Rust Programmers" articles out there.Why would a Rust programmer learn C? Isn't that basically a regression?