
New school C - jamesbritt
http://radar.oreilly.com/2012/12/c-programming-language-ben-klemens.html
======
ikhare
We use C quite heavily at Bump in our mobile apps. With our recent app Flock,
we wrote nearly all non UI logic in C to be shared between iOS and Android.
This made moving to a new platform much faster and easier than in times past.
A lot of this core code is also used on the server to manage incoming
connections from the clients as well.

Aside from the ability to share code, it also makes sure that we are not using
anymore memory than we really need. Avoiding unnecessary GCs is an important
concern in writing performant Android Apps. Having the core C layer only hold
on to as little memory as possible is a huge win.

On a less objective note: When I left undergrad I thought I'd never write C
again, but it was such a pleasure working with it again. Knowing (almost)
exactly what operations your code is doing is quite liberating after working
with so many other higher level languages and frameworks.

~~~
jlgreco
> Knowing (almost) exactly what operations your code is doing is quite
> liberating after working with so many other higher level languages and
> frameworks.

This is exactly why I still enjoy programming in C so much. Even compared to
other languages that I adore and voluntarily use often, programming in C and
programming in anything else feels like the difference between treading water
in a swimming pool, and treading water in a murky lake. I just have a just a
much greater, rather comforting, sense of situational awareness while using C.

~~~
zanny
I have this same sentiment, but I use C++ just because templates, classes,
smart pointers, lambdas, and namespaces are such huge productivity gains.

~~~
antidoh
I keep on thinking I want to pick up C again (made a living with it 20 years
ago), then I remember all the goodness that C++ adds (made a living with it 10
years ago), then I remember that C++ is huge and cryptic and I get all
depressed again.

~~~
primitur
Learn Lua. I went through the same basic phase as you described, and once I'd
gotten my competence with implementing Lua bindings up and running, I never
looked back. For me, Lua+C is the most elegant combination of language tools
I've used, and as a developer with 30+ years of experience, I've used most of
them.

Lua really, really rocks - especially if you look at it as the domain tool it
is, and not the prepackaged broad-spectrum scripting language that many people
consider it to be ..

~~~
qznc
What do you use for basic data structures in C? Stuff like hash map, priority
queue, sorted set, etc.

~~~
primitur
Fortunately, it is very easy to implement these structures in Lua, if they
haven't already. The basic datatype of Lua - the table - can be treated as a
hash map, a sorted set, a priority queue, very, very easily .. in fact if you
think these things need to be implemented in C, you're missing a big part of
the picture with Lua. The power of Lua as a language is really derived from
the "morphological" nature of the basic table datatype .. Lua has all of the
structures you're asking about, already.

And anything it doesn't have, which one would truly need to derive from the
C/C++ side, is a few simple binding calls away .. but anyway, tables will do
all you ask, and more, if you learn to use them properly.

------
sramsay
Languages are among the more religious topics in modern computing, and
sometimes it can be hard to talk about them without immediately provoking a
tedious flame war. I can only say that for myself, after programming for
twenty years in every hot language you can name (really), that I now find
myself going back to C more and more.

Why? Because while others are writing Javascript-like languages that compile
to Javascript, debating the merits of Rust vs. Go, insisting that a language
without monads is unusable, arguing over whether a language without support
for multiple inheritance can be "real OO," claiming that homoiconicity leads
to mystical union, and proudly stating that x language is "almost as fast as
C" -- while all of this is going on, there are legions of people who have been
quietly building nearly every significant piece of the infrastructure of
modern computing in a language that is now over forty years old.

They don't whine, they don't go chasing after "frameworks," and they don't
succumb to the twenty-one-days model of learning. They just build shit. Lots
and lots of it. In fact, you're all soaking in it.

Like I said, it's a religious subject. But as for me and my house, we will use
C.

~~~
dlisboa
I read these types of comments by some people from time to time, usually from
programmers who have been in the industry for a long time. They're always
semi-derogatory towards new technology and carry a disclaimer the author has
used said technologies. But, always, old ones are better. Usually for the sake
of being older, not any inherent quality the comment has elicited.

I love C above almost all languages you subtly referenced. I disagree with a
lot of what you said, and a lot of what you implied. I just wish I never
become that guy.

~~~
sramsay
"I just wish I never become that guy."

But hopefully you will, and I can tell you why.

After a _long_ and painful apprenticeship, you finally realize that being a
great programmer has nothing to do with the language or its features. You get
to the point where you're completely done learning this or that new technique
(which, by the way, is nearly always an old thing for someone else). Your
"profound enlightenment experience" with Lisp is ten years old; your copy of
Design Patterns looks like an old Bible; you've had dozens of dirty weekends
with stack-based languages, declarative languages, experimental languages,
etc. You've been all of those places and done all of that. Many, many times.

At that point, the pros on cons have fully evened out, and what you want is a
tool that you can completely and totally master. You've stopped searching for
tools without warts, edge cases, and gotchas, because you know those _do not_
exist. What matters is community, ancestral memory, stability, maturity, docs.
Above all, you just want to build something really great.

It is at that point that you become that guy. You might well turn to C, as
many before you have. Maybe, maybe not. Lots of us have.

We aren't crusty old neanderthals, though. We're just at the logical end of
the process.

------
primitur
I've been a C programmer since 1984, and have been happily ignoring a lot of
new-school language fads as a consequence. I've found that there's very little
reason not to use C - it is small, it is fast, and with 30+ years of
experience now, I don't get tripped up at all by any of its thorns.

For me, now, new school C can be expressed in one word: Lua.

Everything I need C++ to do, I can get Lua+C-bindings to do easier, with less
fuss, and more productively in the short as well as long run due to the ease
of use of the language. For almost every modern project I've worked on, the
combination of a powerful C library collection and the Lua VM wrapper to make
it all scriptable has been absolutely unbeatable.

Sure, I write Python .. I've done more than my share of Ruby projects, and I
could definitely spend a few years working in pure C++ if it were necessary.
But the point is that none of these languages have been necessary recently,
with my Lua+C skills ..

~~~
skrebbel
I'm curious, could you write a bit more about how you do this? Do you write
Lua programs with performance-intensive parts written in C? Or more like C
programs with some component wiring in Lua? Where do you draw the line, and
why? And what kind of software do you make this way?

~~~
alinajaf
As a newbie C developer I'd for one would really appreciate a follow-up to
this if primitur has the time.

~~~
primitur
Write a nice, clean C library and export a decent header. Then, use the tolua
tool to read that header and generate the bindings for the Lua VM. Build the
LuaVM with those bindings, and suddenly you have the features of your C
library available to you within the Lua language environment.

There are some great tutorials on this, here are a few I found with google-fu:

<http://lua-users.org/wiki/BindingCodeToLua>

<http://csl.sublevel3.org/lua/>

[http://www.nicolasgoles.com/2012/08/03/lua-tutorial---
c-bind...](http://www.nicolasgoles.com/2012/08/03/lua-tutorial---
c-bindings.html)

[http://www.gandogames.com/2011/01/tutorial-using-luabind-
to-...](http://www.gandogames.com/2011/01/tutorial-using-luabind-to-integrate-
lua-with-cc-pt-1/)

I think you will see, once you study the above, that there is a vast amount of
power available to you. The Lua VM, written itself in pure C, is an utter joy
to behold, and I encourage every new C programmer to inspect the Lua VM
sources (its only about 12 files..) and see for yourself just what it has to
offer you. C+Lua can be a very, very powerful tool .. and anyone learning C
today would benefit greatly from also doing some Lua bindings, themselves, to
see what I mean ..

~~~
primitur
I just wanted to add that the LOAD81 project is another great way to learn
Lua:

<http://github.com/antirez/load81>

(Yes, _that_ antirez..)

I strongly encourage anyone who wants to learn Lua to check out LOAD81 - its a
lot simpler than MOAI, but reflects the same basic principles.

------
PommeDeTerre
Why do people act surprised that C and C++ are still as viable as ever, and
still very widely used?

Do they not know that basically every important piece of software today is
still written in those languages?

Has this realization somehow been lost over time with the rise of languages
like Java, PHP, JavaScript and Ruby, even though the major implementations of
those languages are themselves built using C and/or C++?

~~~
calpaterson
> Do they not know that basically every important piece of software today is
> still written in those languages?

A blatant overstatement. There's lots of important work done for the CLR (MS,
many others), Java (Sun, Google, Amazon, bits of Facebook, IBM, etc, etc) or
in Python (Dropbox) or in Cobol (banks...sigh). Yeah, there's plenty of
important C/Cpp code but it's probably in a minority of "important", even if
important just means "commercial" or "popular".

~~~
alayne
I disagree. Most operating systems, desktop applications, web browsers, and
fundamental Internet services (web servers, BIND, MTAs, etc) are written in
C/C++. Dropbox is not in that league of important.

~~~
calpaterson
So you're arguing with me over what "important" means. If "important" means
nearer the bottom of the stack, then yeah, you win. I doubt many people see it
that way though.

~~~
paul-woolcock
If by "bottom of the stack" you mean "the code that the top of the stack
depends on in order to run at all," then I would say that he is correct in
what "important" means.

~~~
kbutler
Infrastructure only exists to enable applications, though it's easy to lose
sight of this as a software engineer.

------
runn1ng
When I look at the page of the book itself

[http://shop.oreilly.com/product/0636920025108.do?intcmp=il-c...](http://shop.oreilly.com/product/0636920025108.do?intcmp=il-
code-books-ben-klemens-code-podcast)

what I noticed was

> Use Autotools, C’s de facto cross-platform package manager.

Seeing the word "autotools" sends familiar, but scary shiver down my spine.
After some years with maven and CPAN, I don't ever want to go back to
autotools, no thanks.

~~~
psykotic
It's also hard to think of anything less new school than freaking Autotools.
There's a good kind of old school (C, UNIX, Audrey Hepburn) but Autotools
isn't it.

------
null_ptr
C is still my favourite language of its style, and so far I also worked on
C++, C#, Java, and Python projects. None other is as simple and direct.

Looking at the book's TOC it looks like there's a bunch of interesting ideas
in there, I'm tempted to pick it up.

~~~
jlarocco
FWIW, some of the advice in the book is pretty bad. The most unbelievable is
to ignore memory leaks under 100Kb, but there are a lot of other examples.

The first half of the book covers Git, autotools, Makefiles, and random topics
tangentially related to C. If you're unfamiliar with those, it's an okay
introduction to them, but there's some dubious advice in those sections, too.

~~~
charlieflowers
Thanks for sharing. Now I know not to waste time on the book. If I want to
learn about Git, I'm not looking to learn it from a book about "Modern C".

------
kabdib
I wrote kernel-level code for quite a few years. There, barebones C makes a
lot of sense. (I can make a case for a subset of C++ that avoids many C++
issues, including use of RAII and similar things that get you into trouble in
environments where /nothing/ is free, and you have to think carefully about
the teardown strategy of every single allocation).

Don't confuse "easy" with "simple". That's my latest mantra.

~~~
huhtenberg
> _a case for a subset of C++_

Do elaborate. I come from the same background and I've been contemplating
forking C for several years now. What you said sounds all too familiar to my
own reasoning, so I'd be very curious to know what you have in mind. In broad
strokes.

~~~
jitl
Take a look at the C++ coding standards adopted for high-reliability areas.
For example, the coding standards for the Joint Strike Fighter:
<http://www.stroustrup.com/JSF-AV-rules.pdf>

~~~
kabdib
Those were mostly good (a little bit of crazy, but not much).

The Google guidelines are okay for non-kernel work. I've worked on three teams
that have independently converged on very similar guidelines (at Apple, a
start-up, and a group in Microsoft). I find that convergence quite
interesting.

------
kgabis
I love programming in C, however I'm getting the impression that right now
almost every open job position is for java/.net/web developers. Besides
embedded, where are C programmers needed?

~~~
ramchip
Finance, mobile, desktop applications. In my (limited) experience most people
use a C++ subset though.

------
schiang
C is a great language. I first learned programming with C back in college.
After a few years off, I decided to pick up programming again but there aren't
many resources for learning C. Most of the tutorials out there are for Ruby
and Python. I have been learning Ruby for a while now and I'm now starting to
go back to using C.

I think if there were more educational websites teaching C, more people would
use it. I think new programmers will appreciate coding a lot more if they
learned C instead of using Ruby.

------
digeridoo
> today’s programmer can even go so far as to question the usage of malloc

In old school or new school C, there's every reason to minimize the use of
malloc and it's easy to avoid.

~~~
huhtenberg
Do give the top 5 list of such reasons.

~~~
aidenn0
I'll give one for old-school: heap fragmentation. You can't fragment a stack,
block allocators have predetermined fragmentation behavior, heap is "who
knows" It is possible to malloc() and free() for a very small net-total, and
yet be no longer able to allocate a contiguous region that is a significant
fraction of the heap.

With a modern VM system, it's not quite as big a deal since your heap is
effectively infinite for many applications. It is still an issue on embedded
systems, and was a problem back in the day too.

~~~
alinajaf
Question from a newbie C developer: How can you avoid using malloc? Are you
finding some clever way to do everything on the stack? Or are you perhaps
allocating a large block of contiguous memory and slicing it up on your own?

I've heard of both of those approaches and they sound pretty hard, so I'd be
interested to hear your thoughts/be pointed to good references on the topic.
Thanks in advance.

~~~
psykotic
A lot of the time, the nature of the data and its lifetime makes specialized
allocators easy to use. Not infrequently they can actually be more convenient
than malloc/free when you have a bunch of allocations (think nodes in a parse
tree) with the same lifetime, so their storage can be released as a single
operation. For that example, you can get the same usability with a more
traditional malloc/free interface using something like hmalloc, but if the
parse tree nodes all occupy the same contiguous block, you get it
automatically. And of course it's a lot faster to release a single block (if
you release the storage back to the OS, the page manager will still have some
per-page work for a virtually contiguous block of memory).

Basically, once you have a nailed down system design, it's usually not any
significant extra work to use custom allocators. Where it can suck away your
productivity is during iterative design and development where you're still
figuring out the different kinds of data, what subsystems exist and who owns
what when. But in that scenario, garbage collection is even easier than
malloc/free, so it's not a mortal sin to use Bochs or just leak memory
(heresy!) if you can get away with it--only temporarily, of course, while you
figure out the design.

------
amcintyre
_There is quite simply a vast array of libraries out there that every
developer has access to. As Ben points out at the 10:50 mark, looking at
GitHub we can find something on the order of 150,000 C projects._

That's great, unless your employer/client/etc. is allergic to open source. I
really can't consider something part of the language unless it's in a standard
library that comes out of the box with the major compilers.

~~~
jlgreco
Most anything that you might want will be BSD-style licensed without the
attribution clause. If your employer is allergic to that... then your employer
is an idiot. GPL'd, even LGPL'd, libraries are fairly few and far between.

------
bhrgunatha
So learning C is on my list of things to do in 2013. I'm pretty experienced as
a programmer, but mostly higher level languages than C.

Would this book be a good place to start?

What other recommendations could experienced C programmers offer. I'm not
simply asking about the language, but best practices for memory management and
toolsand above all, practical advice.

~~~
leif
IMHO you're not an experienced programmer, but there will always be someone
below you on the stack, so I should shut my loud mouth before someone puts my
words in it.

To your request, this is not a good place to start. By the table of contents,
it appears to assume a working knowledge of C.

Here are some ideas you can use to design your own "teach me C" course.

Read K&R, and write some simple toys. Write your own "cp". Handle some errors,
add some options. Try out fread(3)/fwrite(3), then try read(2)/write(2).

Then write "mv" and do the same. Now make it fast within a filesystem. Or work
across filesystems (depending on your first implementation).

Then write an encryption or compression program.

Now write a simple library, make a red/black tree. Store ints first, but then
later, store generic objects (design your API carefully!). Make two iterators,
one using a callback and one using a next() function. Decide which you like
better.

About now you may start to find your development environment lacking, do you
use Make? Autotools? Something else? Go learn one and update your old projects
to use it. While you're at it, write some tests for them. Did you handle
command line options for your version of "cp"? Did you use getopt_long(3)?
Might be a good idea. Oh by the way, does it support "-R"? Hmm...and your tree
isn't reentrant either, better go learn about pthread locks.

After a while, you should start learning some other libraries (because you
want to do real work): curl, libxml, sqlite (hey these are in that book!).
Study their code, their build systems, their test harnesses (oh man,
especially sqlite's) and incorporate the ideas you like into your own work.
Use them to build something cool, maybe a web crawler or whatever.

Do you like music? Learn an audio library and make something. From here, you
should direct yourself to what you like building, because you probably have
enough tools to do the learning on your own.

HTH

~~~
subsection1h

        Read K&R, and write some simple toys.
    

The comp.lang.c FAQ was very helpful when I taught myself C using K&R. It
answered most of the questions that I had that I couldn't answer with K&R
alone. I ended up reading the FAQ straight through because it was so helpful.

<http://c-faq.com/>

~~~
leif
Oh most definitely. Can't believe I forgot it.

------
wmat
And here's a review of '21st Century C' from Andrew Binstock at Dr. Dobb's:

[http://www.drdobbs.com/architecture-and-design/developer-
rea...](http://www.drdobbs.com/architecture-and-design/developer-reading-
list/240145159#)

Note that the review is on Page 2 of the article.

------
sh_vipin
I have seen no system failing because it used C instead of C++. But I have
seen many systems failing because of the improper use of C++.

\- <http://syncfin.com/facebook-promote/>

~~~
JoeAltmaier
Systems in C are not as ambitious, for good reason. So not a fair comparison.
I've never seen a gingerbread house fall in; but lots of wood ones do!

~~~
sh_vipin
This might enlighten !!

[http://unix.stackexchange.com/questions/23634/while-there-
is...](http://unix.stackexchange.com/questions/23634/while-there-is-c-why-
most-of-linux-programs-are-written-in-c)

~~~
JoeAltmaier
No, that does not enlighten. I've written OSs, kernels, embedded systems - in
C++. Because its a better language for those applications. The arguments in
that link are spurious - just sound bytes to make the author feel better.

In no case does C++ require any more runtime than C. You can use little or no
runtime at all in either language, with minimal effort.

Easier to write low-level in C? This is unsupportable. C++ can write anything
you can write in C (its a superset), so anything possible in C is possible in
C++, but now you have more leverage.

~~~
sh_vipin
That's what even I am saying : It is about personal choice. If you like C++
use C++ nobody is stopping you. But that doesn't mean all other languages are
bad or are pathetic.

"Enlightening" was for realizing that debate can never end but realization can
come that both languages are good in their own space. None is like superior
than other at least in case of C and C++.

------
eberfreitas
Slightly off-topic but as a primary PHP developer, how can I get into C? Are
there any free resources or good books to get started?

~~~
antidoh
K&R, and Harbison & Steele were my two favorites.

HN says that Learn C The Hard Way by Zed Shaw is good.

[http://www.hnsearch.com/search#request/all&q=%22learn+c%...](http://www.hnsearch.com/search#request/all&q=%22learn+c%22&start=0)

<http://news.ycombinator.com/item?id=1027549>

As with any new language, once you're comfortable with a small core of the
language, start working on your own problems. Start with little made up
problems, and move on to useful tools as soon as possible. You don't have to
be a master to write your own tools, and you won't become a master until after
you've written your own tools.

