
Ask HN: Imagine C was the only programming language you had. What would you do? - andrewstuart
If you had to do your day to day development in C, what would you do to make yourself more productive?<p>Are there tools, frameworks, libraries that can supercharge modern C development and make it easier, quicker to code, safer and more full featured?<p>Can modern web applications and REST API&#x27;s be built in any practical way using C, or is it just too painful?
======
angersock
Been there, done that.

Some helpful things:

 _Make use of modern standards and libraries_. For example, use <stdint.h> to
get sane and guaranteed-width integer types.

 _Use functional programming style wherever possible_. Function pointers
combined with void pointers lets you emulate closures with explicit context
binding--something that's pretty powerful. Functions that map over collections
and filter over collection structs are powerful.

 _Write a solid unit test macro framework_. In less than 200 lines of C you
can setup a project that will let you easily add unit tests and make sure your
code is working.

 _Hide platform-specific things behind functions and typedefs_. Writing a
simple cross-platform binding to OS primitives like mutices and files and
whatnot can save you a lot of trouble if you ever need to port, as can any of
the libraries out there that provide such things. My friends and I have a
pretty well-vetted one we like to use for these projects.

 _Spend time writing safe arithmetic routines_. Writing a routine that lets
you do different-sized integer addition and multiplication while catching
overflows is _invaluable_ if you are ever parsing or writing binary formats.

 _Abandon your error handling_. C can be made very, very verbose if you
continually check for errors. For example, consider how often you can actually
recover from a failed malloc()--and then consider that Linux will just plain
lie to you. So, don't bother sprinkling all of your code with "well, tell
somebody we bombed out" unless it is truly fixable in the context of the
caller.

 _Consider macros for a DSL_. The simple macros in C are not amazing, but can
be used with a little thinking to build things similar to Flask route
annotations or Java Hibernate annotations.

 _Allocate memory all the time_. Don't mess about with buffers which might get
overfilled or cause you to chunk things...just accept that you'll allocate
frequently and trust in libc until you _know_ you've got a problem.

Those aren't so much libraries as just techniques, but I think that covers a
lot of what helps write C code quickly.

~~~
hagope
That's pretty good advice… Can you point to a source that does this pretty
well?

~~~
angersock
If I had more sources which had done it well, I'd never had learned in the
first place! :)

For some of the safe arithmetic, this page has good tricks:

[http://graphics.stanford.edu/~seander/bithacks.html#CopyInte...](http://graphics.stanford.edu/~seander/bithacks.html#CopyIntegerSign)

(note that _Hacker 's Delight_ is also handy!)

The Half-Life 1 SDK has some interesting examples of macro usage, though it's
in C++. In general, a common place for people to use macros is defining state
machines for game AI.

The Flipcode archives also have some old tutorials and articles which are
handy: [http://www.flipcode.com/archives/](http://www.flipcode.com/archives/)

Go read through the Quake 3 source code to learn about how to structure a
large codebase in C for realtime apps (look especially at how the game engine
is separated and traps the syscalls made by the game logic:
[https://github.com/id-Software/Quake-III-Arena](https://github.com/id-
Software/Quake-III-Arena)

The Linux kernel has some expert-level macro usage and organization as well.

~

I've cited a lot of game code here, because that's what's probably most
accessible, and is where I cut my teeth doing stupid things and learning.
There are doubtless better places (especially for best practices), but I think
that open game code is a good starting point.

EDIT:

Dr. Dobb's had a good writeup on the unit testing stuff:

[http://www.drdobbs.com/testing/unit-testing-in-c-tools-
and-c...](http://www.drdobbs.com/testing/unit-testing-in-c-tools-and-
conventions/240156344)

That's a bit different from how we've solved it in the past, but still good!

------
15155
> Can modern web applications and REST API's be built in any practical way
> using C, or is it just too painful?

Sure, you can write anything in any Turing-complete language.

I've never seen one, but I am sure a C Sinatra clone exists. Quick googling
finds things such as:
[https://github.com/tyler/Bogart](https://github.com/tyler/Bogart) (while
written as a joke - this should illustrate the possibility. afaik tyler is a
reader here)

Possible: absolutely.

The question that needs to be asked, though, is: "Why?"

There are far better-suited (task: web application servers) languages that
compile to C (Nim is a great example). Nearly every language has C interop in
some capacity (at the very least, FFI to C. In the case of Rust and select
others, the capability of compiling with C-compatible ABIs.)

------
S4M
> If you had to do your day to day development in C, what would you do to make
> yourself more productive?

I wish I could come up with the idea of writing a scripting language in C
during my spare time.

------
rdc12
You could do a web-app in C if you really wanted to, but you wouldn't be
playing to the strengths of the language. But you could write a compiler or
interpreter for a new language that is well suited to that... But then you
would be just reliving history.

------
piouokjghfbfg
Huh? So, what, you think that C can handle writing the language you write your
web app and similar in but somehow it suddenly falls apart when transferring
text back and forth across a network socket?

Anyway, I would write a library akin to the STL, then write a library that
overlays that which handles whatever I needed to do (id est HTTP library with
an extension that handles REST/etc).

Then I'd sell all of the extra servers I no longer needed and hire actual
programmers to replace you.

~~~
cbd1984
> So, what, you think that C can handle writing the language you write your
> web app and similar in but somehow it suddenly falls apart when transferring
> text back and forth across a network socket?

Well, if you care about buffer overflows, and you are a human, as opposed
to... no, actually, a computer can't statically check to ensure that your code
has no buffer overflows, either. You need dynamic instrumentation to do that,
which C doesn't give you, or at least not standard C, as opposed to some
C-like language we can define, at which point we might as well go all the way
and write in Rust or Python.

> Anyway, I would write a library akin to the STL

C, not C++. C doesn't have templates, which makes a Standard Template Library
quite meaningless.

------
AnimalMuppet
What do you want to do? Do you want to use C, or do you want to do web
programming? C is by far _not_ the best tool for doing web programming. Yes,
it can be done, but it will be much more pain than necessary.

Or are you in a situation where for some reason you _have_ to use C, and you
still need to do web programming?

------
brudgers
Write a Lisp interpreter?

~~~
andrewstuart
I knew there would be plenty of responses like this i.e. "I'd do something
else apart from C?" Ha!

~~~
brudgers
How else do I get Emacs?

------
airza
Having one-off internet facing applications written in C as the norm sounds
great to me, but I test the security of one-off internet facing applications
for a living :)

------
zimpenfish
CCAN has some potentially helpful snippets and libraries.

[http://ccodearchive.net/list.html](http://ccodearchive.net/list.html)

------
_RPM
I'd have a job quick. I'd have fun projects to work on quick. I'd be at an
advantage over all the hip and cool kids who know JavaScript.

------
gherlein
Be happy!

