Take the 2-minute tour ×

I'm interested in getting into C to get close to the metal performance, but would like to write in a Pythonic style and don't want to roll my own dynamic strings, lists, and dictionaries. Cython is pretty good, but would like to know how to use dynamic variables in straight C if possible.

With C++ there is of course the STL, which will give you String, Vector, and Map. Certainly one possibility is to program in a C-like style in C++, using only those features. Is that the standard practice if you need dynamic variables in C?

share|improve this question
-lpythonX.Y *runs* –  Ignacio Vazquez-Abrams Dec 31 '10 at 18:53
I'm interested in getting into C but would like to write in a Pythonic style. Don't. This is C, not python. They're vastly different. If you want to write python, write python. If you want to write python in C, you'll just end up with horrible code. –  Falmarri Dec 31 '10 at 18:54
@Falmarri: Certainly this is true, but at the same time it can be helpful to take the techniques learned in one language and apply it in another. Provided it doesn't make your code look like crap, of course. –  Ignacio Vazquez-Abrams Dec 31 '10 at 18:57
@R: That's simply not true; Python has the serious overheads of GC, a high-level VM and dynamic typing, which makes the same algorithm written similarly in Python and C almost categorically much faster in C. –  Glenn Maynard Dec 31 '10 at 20:33
show 5 more comments

2 Answers

up vote 3 down vote accepted

Use C++; it has standard and highly-optimized versions of all of these. There's absolutely no reason or benefit to limit yourself to C.

(ed: In other words, yes, that's a very standard practice. Remember, there's no requirement to use any of C++'s features when using C++; by design, you can pick and choose. I often disable exceptions, for example, since it leads to massively bloated executables. There's simply no reason to write code in C.)

share|improve this answer
Many linux libs are written in C, and there are some people arguing that is more appropriate than C++ - isn't there a reason for that? –  7vies Dec 31 '10 at 20:13
@7vies: People who have been using C for decades, who have dug trenches in their habit and history. C was my first serious language, but today the only places I'll use it are 1: when contributing to an existing project that's in C, and 2: if a platform simply lacks C++ support (eg. some embedded systems). –  Glenn Maynard Dec 31 '10 at 20:21
@Glenn: for your second point, too bad gcc cannot "compile" to C. –  7vies Dec 31 '10 at 20:37
I am pretty sure you can still get C++-to-C cross-compilers from somewhere... –  Karl Knechtel Dec 31 '10 at 22:11
@Karl: the problem with "somewhere" is that you can rarely rely on it. –  7vies Dec 31 '10 at 22:47
show 3 more comments

glib is pretty good and widely used:


In fact, glib provides more (much more...) than just ADTs for strings, lists and dicts. But you can easily start by just using those parts, expanding later.

That said, don't think that having dynamic strings, lists and dictionaries will make your code Pythonic. The vast majority of C applications above some level of complexity have implementations of such data structures, but I'm not familiar with any such application written in "Pythonic style".

share|improve this answer
glib has a painfully ugly API; for most typical tasks, the C++ standard library leads to much more readable code. –  Glenn Maynard Dec 31 '10 at 19:08
Bad advice, the worst code I've seen was from people rooted in glib. Start using C++ facilities, standard library, boost, etc., don't start with C and #$%y libraries. –  Gene Bushuyev Dec 31 '10 at 19:16
@Gene: Boost (or much of it, at least) isn't an improvement; the ugliest C++ template abuses imaginable are found there, tied with crypto++ for the worst C++ code you're ever likely to see. –  Glenn Maynard Dec 31 '10 at 19:40
glib is basically the worst of both worlds (C and C++). Either write real C code, or use C++. Just like with natural languages, trying to translate the idioms of one programming language to another doesn't work; it generally results in extremely inefficient and ugly code. –  R.. Dec 31 '10 at 20:16
@7vies: Some of Python's semi-functional constructs are fine, but, for example, almost every case of map(lambda... in Python is much clearer when written as a list comprehension. The C++ functional-style functions like std::bind1st almost instantly become hard to follow. –  Glenn Maynard Dec 31 '10 at 21:48
show 6 more comments

Your Answer


