20 ms·
C for Python programmers (2011)
- gallerdude 10y agoSoon 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.
- yarou 10y agoTake a look at K&R C, and work through the problems without looking at the answers. Though it's a bit dated, I'd still recommend it to anyone who wants to learn C.
- CardenB 10y agoAbsolutely agree. It's not a long book either.
- AdmiralAsshat 10y agoIt's not a long book, but it certainly isn't an easy book, either. I've been doing C for several years and some of the exercises still give me pause.
- kensai 10y agoYou can also get the answer book.
- acomjean 10y agoK&R = Brian Kernighan and Dennis Ritchie. The book is small and pretty good. I found it useful. C is a small language thus the book is small (I think they say something to that effect in the forward). Its book famous enough to have its own wiki entry: https://en.wikipedia.org/wiki/The_C_Programming_Language https://en.wikipedia.org/wiki/The_C_Programming_Language
- jstimpfle 10y agoIt's actually required if you want to write efficient software. Not saying it can't be done e.g. in Python, but you have to know what's going on underneath.
- BipolarElsa 10y agoI took C first and then Python. Would not recommend. Was like going from a Mountain to a Mole Hill. If it were the other way around, the Mountain would not have been as daunting. Just my 2 cents.
- mi100hael 10y agoThis is oft-repeated, but I don't really agree in most cases anymore. These days 90% of software like web application front ends & back ends, mobile applications, desktop applications, data pipelines, etc. are all written with some library in a language like Java, C++, Obj-C, Python, Ruby, or maybe Go. Arguably modern software is going to be more efficient for the vast majority of cases if the author sticks to the best practices for their chosen platform/library than trying to roll their own C implementation or tweak what's going on underneath. Writing C from scratch for anything other than an embedded device or low-level system library is probably ill-advised.
- jerf 10y ago"Java, C++, Obj-C, Python, Ruby, or maybe Go." If you want to write efficient software, which is certainly not always necessary, you're looking at a ~50x factor difference between the top and the bottom of that list, which even in this age of "let's just spin up another couple dozen instances" is non-trivial.
- mi100hael 10y agoSure, even within languages you can be looking at nearly a 50x difference based on things like framework & database choice. Hence my suggestion that raw language speed should not be a deciding factor for most projects these days. For example, look at the difference between the gemini-postgres and dropwizard-mongodb: https://www.techempower.com/benchmarks/#section=data-r12&hw=peak&test=fortune&s=1&l=9r6ek https://www.techempower.com/benchmarks/#section=data-r12&hw=...
- sn9 10y agoJust take Harvard's CS50x on edx. It's probably the best intro to programming online and primarily uses C. Depending on your university's CS program, it might cover the equivalent of 2-3 semesters' worth of material without feeling like drinking from a firehose. If you already know how to program, then you could just pick up K&R and work through it.
- Chromozon 10y agoHopefully 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 10y agoThat 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++.
- jjawssd 10y agoTwo totally different languages with two totally different use cases. Their use cases should not overlap if applied correctly. C++ is a very high level language compared to C. C++ has low level constructs which closely resemble C but should only be used for high performance implementations of high level abstractions.
- protomikron 10y agoYou are right, it is crucial to emphasize to beginners that these languages are different (and C++ not being some kind of add-on to C, which is - considering its name - a valid guess, but we have the same discussion with Java and JavaScript). But I think learning C, Python and JavaScript (and maybe some Java) to a degree that let's you get useful work done, is a lot simpler (meaning takes less time) than learning C++. Not that I would discourage anyone from learning C++ (ok, probably, I might) I just consider it a bad PL for people starting out with programming.
- jstimpfle 10y ago"Python for C programmers" would probably make much more sense (following "C for Assembly programmers").
- Declanomous 10y agoI agree. My computer science classes were taught almost entirely in C. Learning C-like languages is pretty easy, but I constantly feel like I'm using Python incorrectly. I feel like I'm trying to take what is elegant in C and implement it in Python, even though what is elegant in Python is very different.
- 0xffff2 10y agoI think it's largely the opposite for current students. My university's CS program is taught entirely using Python and other higher level languages. CS students here don't have to learn C at all anymore; if you want to learn C here, you have to get a Computer Engineering degree.
- santaclaus 10y agoAren't most OS courses taught in C? At least two years ago when I took operating systems (in the US) we had to hack up Linux source in C. Edit: And thinking on it our computer graphics course was in C++ (yea yea C != C++).
- acomjean 10y agoMost OS are written in C. Thus to interface to get good use from the OS you use the libraries which are in C.. (To get networking, shared memory, message passing, process scheduling and all that good stuff). I wrote c wrappers for ada, and it was a pain. Much easier to use C (or C++) to get to OS functionality. As long as OS are written in C, it will be with us. Also useful for embedded. see: /usr/include
- jlarocco 10y agoThere's a huge variety in CS programs and you can't really assume anything about the languages they all use. My understanding is that accreditation for CS degree programs is based on concepts, not implementation details like programming language. My OS class (a while ago) used assembly language for a simple VM, with one or two assignments requiring us to modify the VM itself to implement new instructions required for new OS features (task switching, virtual memory, etc.).
- krat0sprakhar 10y agoI'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/
- greenshackle2 10y agoI enjoyed K&R C, but I know it's a bit outdated - I was thinking of picking up a more modern C book. Build Your Own Lisp implicitly claims you don't need to know Lisp to learn from the book: > We will be covering many new concepts, and essentially learning two new programming languages at once. Do you think that true? Did you know Lisp before reading it? (I've done the first half or so of SICP so I know some Lisp.) Right now I'm working on designing and implementing a virtual 16 bit CPU and an assembler, I guess the logical next step would be an OS class, then something like Build Your Own Lisp. (For context, I write Python applications at work, but the work done by our embedded system C guys seems more interesting; I'm trying to learn enough C that I could be useful on one of their projects.)
- krat0sprakhar 10y agoYou will end up learning only that subset of lisp that this book implements. So this book is definitely as not overwhelming as it sounds. In my case, I did know some lisp (Clojure) before starting out, but I'd strongly recommend that you don't hold back based only on that requirement.
- sn9 10y agoA slightly more modern book that's a really excellent book to follow K&R is Hanson's C Interfaces and Implementations. It's a book written in a literate programming style that describes how to build a flexible and modular library of data structures.
- mi100hael 10y agoAs 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 10y agoThere 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
- nichochar 10y agoPeople 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 10y agoNot 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.
- payne92 10y agoOh how we've come full circle: in the "old days", this would be "Python for C programmers".
- tedunangst 10y agoNo 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.
- Kip9000 10y agoWhat 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 10y ago...and it can be embedded into Python or embed Python.
- denfromufa 10y agoThe 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.
- avg_dev 10y agoIs there anything like this for Rubyists?
- giancarlostoro 10y agoLet 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-sweet-spot-between-python-and-c/ https://blog.experimentalworks.net/2015/01/the-d-language-a-... 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 10y agoAnother 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.
- deleted 10y ago[deleted]
- jibreel 10y agowhile 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/
- sangnoir 10y agoOfftopic: 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
- poseid 10y agonot 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/
- skoczymroczny 10y ago"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])
- kbart 10y agoIt works, but keep in mind that arrays != pointers.
- uryga 10y agoI remember trying that, and I think it only works in the scope where you declared the array. So for example if you pass the array to a function, the size info is lost. Might be because of the "arrays decay to pointers" thing
- cluoma 10y agoThis only works for arrays allocated on the stack, not for heap-allocated arrays via malloc or similar.
- sjmulder 10y agoI 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.
- westurner 10y agoLearnXinYminutes 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