
C for Python programmers (2011) - bogomipz
http://www.toves.org/books/cpy/
======
krat0sprakhar
I've been programming in Python for a long time and recently took up an OS
class which exclusively used C. Syntactic differences aside (declarations in C
can get pretty hairy), the steepest learning curve while writing anything
useful in C is with a) pointers and b) memory management which this guide
doesn't seem to cover.

From my experience, the best way for learning C has been [0] Build Your Own
Lisp and Zed Shaw's [1] Learn C The Hard Way.

That and of course spending countless hours debugging segfaults and memory
leaks.

[0] - [http://www.buildyourownlisp.com/](http://www.buildyourownlisp.com/)

[1] - [https://learncodethehardway.org/c/](https://learncodethehardway.org/c/)

~~~
protomikron
IMHO pointers are mostly hard at the beginning because the asterisk-operator
is overloaded: On one hand it declares (and optionally initializes) a pointer
(type * x = ...;) and on the other it dereferences it (type y = * x;). At
least for me that was the thing that puzzled me the most. I would prefer
something like that:

    
    
      // declare and initialize x
      @uint32_t x = 42;
    
      // dereference x
      uint32_t y = ~x;
    

I.e. have different symbols for the type and the dereferencer ('@' and '~' as
an example).

//edit: thanks for the help, fixed snippets - HN's parser does not get
pointers either apparently :)

~~~
roel_v
I've been programming C++ for too long to really understand why people find
pointers hard, but I do remember that I did too at the beginning, and I do
remember my epiphany (reading on the train, funny how I remember such
senseless details 15 years later yet can't memorize a 4-item shopping list):
given a declaration of

    
    
        struct Foo {
            int a;
        };
        Foo* f;
    

, the following two are equivalent:

    
    
        int b = f->a;
    

and

    
    
        int b = (*f).a;
    

Never had it spelled out for me like this, probably because it's so obvious
once you get it - thought I'd share in case it gives anyone else an 'aha'
moment.

~~~
acbart
The trick to teaching pointers is to model memory with boxes. You have a
stack, and a heap, and you draw arrows to represent pointers. At some point,
you need to reveal that those arrows are just numeric indices, but the visual
helps early on. The * and & operators just let you follow the value in the
pointer. I've had a number of students who were baffled up until I showed them
this model.

Notational Machine for the win!

~~~
userbinator
In general, thinking of memory as an array of bytes is helpful, and each byte
has an address which is itself a series of bits that can be stored in memory
--- and the concept that ties it all together and what I find makes a lot of
people just "get it" is the fact that memory can itself store addresses that
denote locations within it.

~~~
Anthony-G
That's a perfect example of how knowing _some_ assembly code helps with
learning C.

------
tedunangst
No mention of malloc or the struct keyword? You'll probably want to learn
about that before dealing with real C code.

> #define forever while(1)

> Expert C programmers consider this very poor style, since it quickly leads
> to unreadable programs.

So why even mention it??? There are more important subjects which could have
been introduced in this space.

------
jstimpfle
"Python for C programmers" would probably make much more sense (following "C
for Assembly programmers").

~~~
mrfusion
But how do you learn assembly?

~~~
InitialLastName
Assembly Programming for Chip Designers

------
payne92
Oh how we've come full circle: in the "old days", this would be "Python for C
programmers".

------
sangnoir
Offtopic: when I saw "C for Python programmers", I immediately thought it was
going to be this beaut/horror -
[https://twitter.com/UdellGames/status/788690145822306304](https://twitter.com/UdellGames/status/788690145822306304)

------
nichochar
People are criticizing this, but this is very valuable to someone like me, who
just wants to brush up a bit on my C, having some minor experience, and a lot
of python experience, so thanks!

~~~
canada_dry
Not to diminish the usefulness of this brief intro (it is very well written)
but, as the saying goes "a little knowledge...".

Two of the biggest stumbling blocks in C (and their ilk) are pointers and
memory/garbage collection... which this brief intro barely mentions.

It actually reminded me of many programming courses I've taken over the years
where the instructor spends an inordinate amount of time on the easiest
concepts and quickly glosses over the meaty bits.

------
gallerdude
Soon this will be me - freshman CS student who will be learning C the second
semester. A lot of people say it will be difficult, but there is a certain fun
in building things from scratch.

~~~
Chromozon
Hopefully you will be learning C++ and not straight up C. Modern C++ has
pretty much solved all the shortcomings of C, and it's sad that people are
still stuck learning archaic C for no good reason. If you are actually
learning C++, do NOT even look at K&R C. It is _extremely_ outdated and
straight up teaches bad practices.

~~~
protomikron
That is definitely some kind of [https://xkcd.com/386/](https://xkcd.com/386/)
\- moment for me.

I totally disagree. I am not sure if your comment is satire or not, I would do
it the other way around. Learn C as it is fundamental to modern OSes and just
learn C++ if you _have_ to (e.g. maybe later at a job, or because you need a
specific library, like OpenCV). However I do not think that fully grasping C++
is a worthwile endeavor, as there are many things implemented due to
historical and/or compatibility reasons (to C) and not because they provide a
real benefit. Not that C is perfect but I consider it less fucked-up than C++.

~~~
pjmlp
That is the path to write unsafe and unidiomatic C++.

CppCon 2015: Kate Gregory “Stop Teaching C"

[https://www.youtube.com/watch?v=YnWhqhNdYyk](https://www.youtube.com/watch?v=YnWhqhNdYyk)

~~~
protomikron
Although I am not the big C++ fan, this is a wonderful talk and she has a
great approach to teaching programming languages. Thanks!

------
Kip9000
What could be really interesting for a Python programmer is [Nim Lang]
([http://nim-lang.org/](http://nim-lang.org/))

Python like syntax, statically typed, garbage collected, C like perf.

~~~
nimmer
...and it can be embedded into Python or embed Python.

------
westurner
LearnXinYminutes tuts are pretty cool. I learned C++ before I learned Python
(CPython ~2.0) before I learned C:

C++ ("c w/ classes", i thought to myself):

\- [https://en.wikipedia.org/wiki/C++](https://en.wikipedia.org/wiki/C++)

\-
[https://learnxinyminutes.com/docs/c++/](https://learnxinyminutes.com/docs/c++/)

Python 2, 3:

\-
[https://en.wikipedia.org/wiki/Python_(programming_language)](https://en.wikipedia.org/wiki/Python_\(programming_language\))

\-
[https://learnxinyminutes.com/docs/python/](https://learnxinyminutes.com/docs/python/)

\-
[https://learnxinyminutes.com/docs/python3/](https://learnxinyminutes.com/docs/python3/)

C:

\-
[https://en.wikipedia.org/wiki/C_(programming_language)](https://en.wikipedia.org/wiki/C_\(programming_language\))

\-
[https://learnxinyminutes.com/docs/c/](https://learnxinyminutes.com/docs/c/)

And then I learned Java. But before that, I learned (q)BASIC, so

------
sjmulder
I like the style this uses to explain the else-if construct. A few fundamental
concepts are explained and combined in a way that it not only makes else-if
obvious but also anything else using these fundamentals.

SICP also uses that style throughout and I love that. Wish I could explain
things that well.

------
denfromufa
The best way to learn C for Python programmers is to dive into CPython
interpreter or C-API extensions. Also use Cython annotation outputs to see how
Python translates into C calls.

------
mi100hael
As someone who first learned Python and then picked up C, the two biggest
challenges for me were: string parsing and mixed (or unknown)-type
collections. Took me a good while to change my mindset since those operations
are so easy & widely used in Python.

~~~
faragon
There are many libraries for "high level"-like string operation for the C
programming language, e.g.
[https://faragon.github.io/sstring.h.html](https://faragon.github.io/sstring.h.html)

------
skoczymroczny
"C does not have an support for accessing the length of an array once it is
created"

Well, there is sizeof(array)/sizeof(array[0])

~~~
cluoma
This only works for arrays allocated on the stack, not for heap-allocated
arrays via malloc or similar.

------
poseid
not as detailed, but more on a higher level this post from JavaScript to C
might be interesting for some too: [http://thinkingonthinking.com/learning-c-
for-javascripters/](http://thinkingonthinking.com/learning-c-for-
javascripters/)

------
jibreel
while writing c i found this free book helpful as a reference.
[http://publications.gbdirect.co.uk/c_book/](http://publications.gbdirect.co.uk/c_book/)

------
giancarlostoro
Let me point out two alternatives: Cython which is very Pythonic looking, and
compiles to C and produces amazing projects including UVLoop which is a drop-
in module for asyncio for Python 3 that will speed up asyncio:

[https://magic.io/blog/uvloop-blazing-fast-python-
networking/](https://magic.io/blog/uvloop-blazing-fast-python-networking/)

[https://github.com/MagicStack/uvloop](https://github.com/MagicStack/uvloop)

Note how GitHub claims it's all mostly Python code ;) That's because Cython
like I said looks Pythonic.

There's other examples, but I think this is one of the one's that come to mind
the most to me.

There's also D which is called Native Python by some (unlike projects like Go
and Rust, you can have your Object Oriented Programming (optional like in
C++), and concurrency / parallelism too and other goodies like built-in unit
testing, when you compile your code your unit tests are ran but not included
in the final binary):

[http://bitbashing.io/2015/01/26/d-is-like-native-
python.html](http://bitbashing.io/2015/01/26/d-is-like-native-python.html)

[https://blog.experimentalworks.net/2015/01/the-d-
language-a-...](https://blog.experimentalworks.net/2015/01/the-d-language-a-
sweet-spot-between-python-and-c/)

If it's been more than a few years since you've evaluated D you might want to
check it out again, it may be worth your time. D is a language I knew about
for years, and recently is where I've come to appreciate it for it's many
features.

D has things like array slicing, [OPTIONAL] Garbage Collection, an amazing Web
Framework called Vibe.d with it's own template engine called Diet-:

[http://vibed.org/](http://vibed.org/)

[https://vibed.org/blog/posts/introducing-diet-
ng](https://vibed.org/blog/posts/introducing-diet-ng)

Things I like is Vibe.d is not just a Web Framework but a full networking
stack too, also it supports Pug / Jade like templates (see Diet-NG) you make
and compiles them when you compile your project, so your website runs off a
native executable using fibers instead of threads. Vibe.d is undergoing a
period where the core is being rewritten to where it is more compartmentalized
so that you pick and choose which parts you need, MongoDB, PostgreSQL, layout
engine and other goodies, there's even a templating library whose syntax is
based around eRuby called Temple (though the syntax can be tweaked) that
supports Vibe.d:

[https://github.com/dymk/temple](https://github.com/dymk/temple)

~~~
dagw
Another vote for Cython. All python programmers owe it to themselves to take a
look at Cython. The fact that it's compatible with (almost?) all existing
python libraries and you can almost certainly run your existing python code
without any changes to the source code makes it very easy to get started with.
Then you can start making (often relatively minor) changes to your source code
and often get 10-100x speed ups on CPU bound tasks. And the resulting
libraries are all automatically importable back into normal cpython.

------
avg_dev
Is there anything like this for Rubyists?

