News for hackers. Stuff that matters.
Re:Actually Portable Executables by ducktective on Sunday February 28, 2021 @06:24AM (#26292571)
Any recommendations to somebody who wants to learn C "properly" in $CURRENT_YEAR?
Re:Actually Portable Executables by CraigJPerry on Sunday February 28, 2021 @08:58AM (#26293446)
https://www.oreilly.com/library/view/21st-century-c/97814919...

I thought 21st Century C was good, i've still kept my copy. I'd happily recommend it.

I like the K&R book too - it feels reeeeeeally old but it's really short.

There's a few others that have helped me in various ways but these are a little older -

Love C by Tim Love (free online, my copy is something i just printed out, it's not that long).

Programming from the Ground Up (x86 assembly) - this is available freely online but i bought the book and that helped me a lot with C even though it's a book with only assembly... (to be fair, it does go through calling conventions).

Finally there's another book i love, Advanced Programming in the Unix Environment by Stevens, i have the 6th edition updated by Rago after Stevens' passing. Fascinating book - but huge.

Re:Actually Portable Executables by jtvjan on Sunday February 28, 2021 @06:42AM (#26292664)
I really liked "The C Programming Language (second edition)" by Kernighan & Ritchie. It doesn't teach C99 and later features, but if you want to write portable code you're not using those anyway.
Re:Actually Portable Executables by e12e on Sunday February 28, 2021 @08:56AM (#26293434)
From various discussions on Hn, I think the consensus is most people really like k&r as a technical book (it's concise, clear and a fun read) - but everyone working with C says its a terrible book for learning modern C.

I tried to refresh my memory from previous threads, and I think the general trend has been to recommend (as seen in sibling comments):

https://modernc.gforge.inria.fr/

https://nostarch.com/Effective_C

I've also seen the older:

https://www.oreilly.com/library/view/21st-century-c/97814493...

Mentioned.

There's also Architecture of Open Source Applications, which can help with starting to read some larger code bases, some of which are in C: http://aosabook.org/en/index.html

And there's a general recommendation to go read the source code of the Redis cache/db.

Finally i came across a mention of this short article on gdb (nb: mention of TUI text ui should probably have been in the top, not a foot note):

https://www.recurse.com/blog/5-learning-c-with-gdb

I feel I'm missing a book that has come up often, but can't think of which one.

I did like zed Shaws learn c the hard way, but I'm afraid it's getting a bit long in the tooth: https://learncodethehardway.org/c/

Re:Actually Portable Executables by sillysaurusx on Sunday February 28, 2021 @08:22AM (#26293239)
K&R is a horrible way to learn C. It's the best way to make someone run in the other direction, though.

It's like telling someone that SICP is the best way to learn Scheme. Maybe, but lots of people learn differently.

Unfortunately I don't have good suggestions myself, but at least they won't feel bad if K&R isn't their cup of tea.

Re:Actually Portable Executables by kamranjon on Sunday February 28, 2021 @11:45AM (#26294599)
I am just trying to learn myself so take this with a grain of salt but I have found the github of Antirez:(https://github.com/antirez) - the author of Redis - to be an amazing resource. The code is incredibly well documented and to my eyes pretty well written. A good place to start I think is Kilo:(https://github.com/antirez/kilo/blob/master/kilo.c) - a 1000 line text editor with no dependencies.
Re:Actually Portable Executables by himujjal on Sunday February 28, 2021 @09:16AM (#26293544)
APE makes it viable for a cross platform C library with a cross platform C compiler to make cross platform binaries.

Besides I was in the same boat as you. I come from the world of JS/Python/Go. I even wrote https://github.com/Himujjal/ekon in pure C recently. The reason I thought C would be good is performance and portability. But I found it to be better to invest time in Rust rather than in C. C is a fantastic language but cross platform dependency management is difficult. Unit Testing is also difficult. There are solutions but not as efficient as Rust's ecosystem.

BTW I wish there could be a Cargo for every language.

Re:Actually Portable Executables by moonchild on Monday March 01, 2021 @04:00AM (#26300861)
> C is a fantastic language

Odd...I find that a very strange sentiment.

I thoroughly enjoy C, and have even written a compiler for it, but I don't think it's a well-designed language by any account.

Re:Actually Portable Executables by alisonatwork on Sunday February 28, 2021 @11:20AM (#26294387)
I can't remember where I learned the basics, but if you aren't new to programming in general then I think there are only three important things you need to know: what is a pointer, what is a macro, and always free what you malloced. Reading a book or tutorial will be helpful for this. In my day it was K&R, no doubt there are more up-to-date ones now.

But the next step, which I think is far more important, is to look at the source code of tools that you actually use in real life. Things like cp, or wc, or head. You've probably used them for years without thinking about it, but they're all written in C. Don't look at the modern GNU versions just yet, since they can be packed full of complex functionality, start with micro implementations like Busybox or Toybox. Then look at some OSes that are known for super clean code like NetBSD or OpenBSD. In those OSes you have the benefit of being able to go back to the version that existed 25+ years ago, so you can read through the diffs to see how they adopted new features and found new ways to address C's biggest flaw/risk - memory exploits.

If you're interested in kernel programming, check the Linux and BSD sources, and lurk on the mailing lists for a while to see how people talk about the code. The review process tends to be a bit more brusque than you might be used to on Github, but it's often detailed and results in code that is of a relatively high quality, or at least a consistent standard. It's a great place to learn.

Re:Actually Portable Executables by vkazanov on Sunday February 28, 2021 @06:43AM (#26292673)
Try new books: effective c and the free modern c one. They actually teach the language and all the concepts.
Re:Actually Portable Executables by asicsp on Sunday February 28, 2021 @07:55AM (#26293067)
Re:Actually Portable Executables by vkazanov on Sunday February 28, 2021 @01:49PM (#26295927)
Yup
Re:Actually Portable Executables by ktr on Sunday February 28, 2021 @11:19AM (#26294381)
Re:Actually Portable Executables by dj_mc_merlin on Sunday February 28, 2021 @07:24AM (#26292893)
K&R -> do some random programming problems -> small project -> learn ASM -> learn how to do a buffer overflow (read "Smashing the stack for fun and profit") -> from here find your own path.

The first three are just useful for getting the basics. The next two are for starting to learn low level stuff. You don't really understand C until you understand how it relates to lower level code.

Re:Actually Portable Executables by unkulunkulu on Sunday February 28, 2021 @06:35AM (#26292631)
$CURRENT_BEST_BOOK Sorry :)
Re:Actually Portable Executables by CodeArtisan on Sunday February 28, 2021 @09:41AM (#26293688)
C Primer Plus by Stephen Prata is the most complete i have seen, it does cover everything up to C11 and goes in details about stuff like array vs pointer.

I don't like 21st Century C, it may have changed since then but it has a chapter titled "Object-Oriented Programming in C" which is confusing object with Abstract data type.

Re:Actually Portable Executables by edgyquant on Sunday February 28, 2021 @12:43PM (#26295179)
I assume object oriented C means using structs and function pointers?
Re:Actually Portable Executables by CodeArtisan on Sunday February 28, 2021 @04:21PM (#26297181)
No. For 21st Century C, an "object" is an incomplete type and procedures.

    struct Object;
    void Object_init(struct Object *o);
That's ADT.