
Modern C, Second Edition - matt_d
https://gustedt.wordpress.com/2019/09/18/modern-c-second-edition/
======
k_sze
How does this book compare with "21st Century C"?

I know that, unlike "Modern C", "21st Century C" can't possibly cover C17
because that book was released in 2014.

But otherwise, what would be notable differences? In terms of style,
correctness, idiom, depth, and breadth?

~~~
AlexeyBrin
C17/C18 is a minimal "bugfix release" to C11, mostly standard clarifications.

[https://en.wikipedia.org/wiki/C18_(C_standard_revision)](https://en.wikipedia.org/wiki/C18_\(C_standard_revision\))

------
syphilis2
Is there a place to report errors? I've just skimmed through and the pdf has
some possible formatting bugs: tables inside code blocks, page breaks
splitting the tops of code blocks, small things like that.

~~~
bloodytux
[http://modernc.gforge.inria.fr/bugs.html](http://modernc.gforge.inria.fr/bugs.html)

~~~
Jedd
Which just redirects to this:

[https://gforge.inria.fr/tracker/?group_id=6881](https://gforge.inria.fr/tracker/?group_id=6881)

But the 'Bugs' link is literally on the first page of the book.

------
skocznymroczny
I'd love a language that feels like a more convenient C. C as it is, with some
niceties like built-in containers with some nice syntax sugar. Maybe someday
Zig will get there.

~~~
gnode
Why would conservative use of C++ not meet this mark? It's highly compatible
with C, has container types, ranged for-loops, operator overloading which
makes use of container types more sugary.

~~~
DagAgren
"Conservative use" introduces a massive cognitive load of trying to figure out
what you are and what you aren't using.

And the ergonomics of modern C++ look absolutely horrid compared to actual
modern languages.

~~~
humanrebar
How is it a massive cognitive load? Use clang libraries and error out if there
are any operator declarations, for instance.

~~~
AndrewGaspar
"How is it a massive cognitive load? Just write a bunch of tooling to enforce
your rules!"

~~~
humanrebar
It's simpler than the tutorial example for python-clang. It's strictly less
work than a responsible unit test suite, and there's not much downside to
getting some details wrong on this particular application.

~~~
heinrich5991
>tutorial example for python-clang

Do you have a link? Your comment is the highest Google result for that quote.

------
fxleach
I guess no pre-ordering, the link is broken:
[https://www.manning.com/books/modern-c](https://www.manning.com/books/modern-c)

~~~
arthurcolle
PDF is here:
[https://gforge.inria.fr/frs/?group_id=6881](https://gforge.inria.fr/frs/?group_id=6881)

~~~
markdoubleyou
Wow, I wish more technical books used LaTeX for typesetting like this.
Beautiful.

~~~
mikece
Since you mention typesetting: I am working on a volunteer project that is
transcribing old, out of print books from the 1700s through early 1900s (all
public domain). For now the plan is simply to make them available online
formatted for web, e-readers, and PDF, but thought is being given to
formatting for print-on-demand. We've never done the latter so we have no idea
what tools would be best for that: is there a good reference for free
tools/software for doing page layout for physical books?

~~~
photojosh
Any connection to
[https://standardebooks.org/contribute/](https://standardebooks.org/contribute/)
or if not, are you online yet? Wouldn't mind having a look. (And also
interested in the print typesetting question.)

------
Santosh83
On a related note, do we have any fully complaint C11 compiler for MS Windows
or is using gcc under WSL (or a VM) the best option? I take it there is no C17
compliant compiler yet?

~~~
jeremyjh
You don’t need WSL to run gcc on windows. You can use the mingw distribution
to get a GNU tool chain that compiles a standard windows EXE.

~~~
Gibbon1
Stuff people 'just know'

mingw: For native build and execution. A lot of Windows IDE's will just work
with mingw.

msys2: For posix compatible build but native win32 targets. Useful when your
project uses autotools. Like when trying to build someones library. msys2 just
tends to work. Trying to get autotools to work under windows tends to exceed
my patience, time and brain cells.

cigwin: for a posix compatible build and runtime environment.

Also gcc can be built as a cross compiler. The two times I tried it is was
more straight forward that I would have thought.

~~~
badsectoracula
Note that msys2 isn't just for building programs, it provides a full bash
environment with many unix tools and a package manager (pacman, same as in
Arch Linux) to install more. The build environment is unixy enough to be able
to build more tools from source using the classic ./configure; make; make
install route (more or less, may need some modifications in the code).

For me my main use of msys2 is as a shell where i run scripts and command-line
utilities with the build stuff being a distant secondary use.

------
ausjke
The Linux Programming Interface: A Linux and UNIX System Programming Handbook
- is arguably the best C programming book nowadays if you code in Linux/Unix,
the rest C books are most for ANSI C, that might be more useful for bare metal
embedded system that does not run a full multi-task POSIX OS?

------
sys_64738
Do people still use K&R these days?

~~~
Gibbon1
If someone tells you to use K&R take that as license to ignore their advice
from that point on.

~~~
valacmur98
I am using it to study C. Can you tell me the reason why you wouldn't
recommend it?

~~~
ufo
K&R is a well written book, and is very pleasant to read. IMO, it is one of
those books that everyone should read one day or the other.

The main thing to watch out about it these days is that K&R is a bit carefree
about the numerous ways where you can shoot yourself in the foot using C. For
example, K&R famously implements strcpy as `while( _dst++ =_ dst++);`. These
days, most people would say that code is excessively terse, and that you
shouldn't be using strcpy in the first place (because it can potentially
overflow the dst buffer).

~~~
einpoklum
You meant:

while ( _(dst++) =_ (src++))

I believe.

~~~
Gibbon1
while ((dst++) = (src++));

~~~
billforsternz
Both of these replies are missing the asterisks to indicate pointer
indirection. Probably HN is eating each asterisk, treating it as emphasis or
something.

Also terse it may be, but this (assuming the missing asterisks are restored)
is a good, idiomatic implementation of strcpy. Whether strcpy is good or evil
is a separate discussion.

------
aportnoy
Is this a good book, can anyone comment?

~~~
torstenvl
I'm not a fan so far. Lots of deliberately bad C code. Using uninitialized
variables, etc.

Explanation of jargon seems to take priority over explanation of the language
- the term "string literal" is explained before the concept of a function.

~~~
tym0
What book would you recommend as an alternative?

~~~
AlexeyBrin
_C Programming: A Modern Approach_ , 2nd Edition by King is a good beginner
book.

 _21st Century C_ by Ben Kelemens is also a good book.

~~~
afroisalreadyin
I would second 21st Century C, it's an excellent book. It has a very pragmatic
approach, skipping features like unions that are not used in modern C, but
going into great detail on the important things like memory management.

~~~
flukus
Thirded, especially on the pragmatism, this by far the most pragmatic
programming book I've ever read. I fell in love with the C as a scripting
language parts
([https://github.com/RhysU/c99sh](https://github.com/RhysU/c99sh)), now I
don't have much of a use for a scripting language to sit between bash and
"proper language" (apart from some occasional awk).

~~~
RhysU
Glad it has been of use!

------
mobilemidget
[https://www.manning.com/books/modern-c](https://www.manning.com/books/modern-c)

Gives me a 404?

------
big_chungus
As long as he's publishing under a CC license, it might be nice to release the
LaTeX source as well. PDF is usually good enough, but sometimes needs to be
converted to other formats; you usually get best results going latex->epub
than pdf->epub. Some devices it's a lot easier to be able to change attributes
to better fit the form factor.

~~~
compressedgas
If the source was available, we could fix all the formatting errors.

------
jakedub4d2
Where is the Holy C edition :) (PS. I know about his rants on ycomb) If you
can get past his rants, some of his concepts end up pretty neat. To experience
it, just download the ~12mb OS and run it in VM Virtual Box pretty quickly.
Choose 64bit alternative OS. [https://www.vice.com/en_us/article/wnj43x/gods-
lonely-progra...](https://www.vice.com/en_us/article/wnj43x/gods-lonely-
programmer)

------
vymague
Link for the code examples on
[http://modernc.gforge.inria.fr/](http://modernc.gforge.inria.fr/) is broken.
Should be .../5298/code.zip instead of 5297, I think.

------
hasahmed
Cool stuff!

Typo in first paragraph of
[https://gustedt.wordpress.com/category/c/](https://gustedt.wordpress.com/category/c/)
(mayor instead of major)

------
ukyrgf
Scandalous language on page 3!

> printf("element␣%zu␣is␣%g,␣\tits␣square␣is␣%g\n",

------
mister_hn
love it, thank you for giving the PDF out for free

------
nullbyte
A little off topic, but that is some terrible art on the book cover.

~~~
Etheryte
To be fair, I don't think I've ever seen a serious, hardcore programming book
that looked nice. They're either neutral, or just downright weird [1].

[1]
[https://en.wikipedia.org/wiki/Structure_and_Interpretation_o...](https://en.wikipedia.org/wiki/Structure_and_Interpretation_of_Computer_Programs#/media/File:SICP_cover.jpg)

~~~
wutbrodo
Aw man I was gonna hold up SICP as a counterexample

------
mrspeaker
Damn it, two giant gross "toenail fungus" ads taking up most of the page -
shocked me a bit and I closed the page really quickly (and probably now
associate this book with fungal diseases - choose your ad providers
carefully!)... Is there a non-fungus-y link to this book somewhere?

~~~
boring_twenties
I was theoretically aware that there are people out there that don't use ad
blockers, but I sure as heck didn't expect to find one on hackernews.

I'd recommend uBlock Origin.

~~~
dlivingston
That's an ethical question that I've weighed a decent bit. I don't use ad
blockers personally, as ads are truly the reason most of the internet is free.
If ads are too intrusive, I will 'vote with my clicks' and avoid that
particular website in the future.

There are privacy concerns with this, of course, but DuckDuckGo and modern
Safari (with cross-site tracking prevention) helps mitigate a bit of it.

~~~
iamnotacrook
"If ads are too intrusive, I will 'vote with my clicks' and avoid that
particular website in the future."

They'll never even notice, and in the meantime you're at risk of all sorts of
trackers, malware etc. Plus...you're seeing adverts all the time. I used
chrome on android the other day and I couldn't believe how many there were, or
how annoying. If you're not clicking on them it means nothing anyway, as far
as I can tell. I remember back in the day sites saying "click on the ads - it
helps us" but I don't think that's a thing any more. Perhaps I should knock up
a script to visit random sites with ads and maybe click on a few, in a
container/vm. Would that help?

~~~
zten
> Perhaps I should knock up a script to visit random sites with ads and maybe
> click on a few, in a container/vm. Would that help?

Don't worry, there's enough publisher-side fraud already doing that for you!

------
ncmncm
Contradiction in terms.

~~~
saagarjha
There’s a lot of new things in C since the K&R book came out.

~~~
ncmncm
Half of them cribbed from C++.

~~~
saagarjha
Not really; C has a number of things that C++ never adopted such as restrict
and partial structure initialization (although this might be coming to C++).

~~~
ncmncm
Half does mean some are, some aren't.

"//" comments, function prototypes, const, atomic, inline, complex, mid-block
definitions are.

Runtime variable local arrays, designated initializers, restrict not. C++
cribbed designated initializers from C, coming in '20\. Restrict was cribbed,
late, from Fortran.

~~~
wahern
> C++ cribbed designated initializers from C

AFAIU C++ will require that you initialize the members _in_ _the_ _same_
_order_ as defined. It also won't permit you to exclude any definitions. Why
even bother?

Somewhat controversially, C permits you to define members multiple times, with
the last definition taking precedence. Compilers sometimes warn about this,
but I've _personally_ found the behavior useful--I'll write an API that
provides a macro with default values yet which allows the user to override any
particular definition. I don't think I've ever had a bug in an initialization,
at least not involving multiple definitions. That seems like a very easy and
superficially useful diagnostic to write, but which prevents useful behaviors;
behaviors that named initializer's were deliberately designed to provide.

~~~
ncmncm
In a C++ designated initialization, members _may_ be omitted. (You were told
wrong.)

There is probably no reason to enforce order unless unmentioned elements have
a non-trivial destructor, so that could be relaxed in a future Standard.
Members that need destruction would need to be destroyed in the opposite
order; enforcing order allows reusing code already generated for the
containing-object destructor, but in many interesting cases (e.g. C structs)
there are no destructors to run anyway. In the others, there is no reason why
it would need to re-use the class destructor.

~~~
wahern
I stand corrected, thank you.

For posterity (I like to leave breadcrumbs for myself):

[https://github.com/cplusplus/draft/blob/dc2bff0/source/decla...](https://github.com/cplusplus/draft/blob/dc2bff0/source/declarations.tex#L4614)

[https://github.com/cplusplus/draft/blob/dc2bff0/source/decla...](https://github.com/cplusplus/draft/blob/dc2bff0/source/declarations.tex#L4693)

[https://github.com/cplusplus/draft/blob/dc2bff0/source/compa...](https://github.com/cplusplus/draft/blob/dc2bff0/source/compatibility.tex#L586)

------
sigjuice
IMHO, the phrase “C is a compiled programming language” is super confusing.

Edit: there are many languages that have both compilers and interpreters.
There are several C interpreters as well. The classification of languages as
“interpreted” or “compiled” does not appear to be a sound concept, IMHO.

~~~
KevinEldon
What about the phrase do you find to be super confusing? It is a short summary
(even labeled as takeaway 0.1.2.1 in the text) of the first paragraph of
section 1.2. That paragraph explains how C source code is just text and that
it is turned into an executable program with a compiler. In context the phrase
seems more than clear.

~~~
sigjuice
The explanation of what a compiler is and what it does is perfectly fine. A C
compiler is how most of us do C. But going to “C is a compiled programming
language” is quite the wrong generalization IMHO.

