25

If this is the wrong place to ask this question, please let me know.

I'm a Python programmer by occupation. I would love to learn C. Indeed, I have tried many times, but I always get discouraged. In Python, you write a few lines and the program does wonders. In C, I can't seem to be able to do anything useful.

It seems to be very complicated to even connect to the Internet.

Do you have any suggestions on what I can do to learn C? Are there are any good websites? Any cool projects?

Thanks

4
  • 4
    It is nice to be able to stand on the shoulders of genius isn't it. The Python interpreter is written in C. Now you have a true appreciation for what the designers really did. It is pretty fing amazing.
    – Pemdas
    Feb 27, 2011 at 2:10
  • @Pemdas C is normally used for creating system programs.
    – Searock
    Feb 27, 2011 at 7:52
  • Might I recommend starting out by reading the contents of a file, and possibly doing some basic transformation into another file? Example: Take a CSV and parse it into a tab-delimited file. Alternately, you could learn how to read header information out of a BMP of JPEG image file. Aug 31, 2012 at 21:31
  • I'm to late to add an answer, but there's this online C for Python Programs booklet. Also, the makers of "Learn programming the hard way" are making a book called "Learn C the Hard Way", which is aimed at people who already know a bit of python or similar.
    – naught101
    Feb 22, 2014 at 12:17

7 Answers 7

35

Don't get discouraged.

Python is a high-level programming language. In comparison to C, it can produce wonders in a tiny amount of code. Don't start by trying to mimic Python results in C - you'll be promptly disheartened. Programming in a C requires a different style of thinking and understanding because as you're interacting with the computer at a more intimate level. Here's a good starting point for learning C:

Books

Online Material

1
  • 1
    Can you suggest a programming exercise? Something useful preferably. Thanks. Mar 1, 2011 at 1:02
15

If you want to learn C, don't start with connecting to the Internet. You can do that in any other language. That's not what's most important in C.

Instead, take a look at pointers, arrays, references, structs in this C Tutorial. This will help you understand memory management and many of the features that "bring you closer to the machine."

If you can learn and understand this material, then look into expanding into a possible project. These concepts are fundamental to any real professional C programming.

This Pointer Tutorial looks helpful as well.

1
  • the C_Tutorial link is broken ... Mar 18, 2019 at 16:52
11

It is very complicated to connect to the internet! Think about it:

  • Your computer might have multiple ways of connecting -- WLAN, Bluetooth, Ethernet, etc. Each of those has its own settings, which you need to set. You're lucky that there's already standards and drivers for these... using these could be a lot harder than it already is right now.

  • IP addresses can be IPv4 or IPv6, and you need to be able to handle both.

  • There may or may not be proxies configured for the system.

  • You have to support FTP, HTTP, or some other protocol.

  • You might need to use dial-up, so you have to specify a phone book with passwords and phone numbers, if you're not already connected. Ditto with WLAN usernames/passwords.

  • You might need to work with cookies.

  • The system might cache some data, so do you want to use the cache or not?

and the list goes on and on.

So the fact that it's easy to do these with Python necessarily means that a lot of these are hidden from you (or difficult to set), so you trade simplicity for power. The C library is the opposite: it has all the power you need (you can easily perform system calls), but doesn't have any defaults (or, in fact, any features for connecting to the internet).

Instead of thinking of C as being tedious, think of it as being powerful. But in any case, don't try to connect to the internet with it -- that's a pretty darn hard thing to do. Instead, start learning how to use the library for more basic tasks (e.g. file I/O), learn about how pointers and arrays really work, and when you learn all those, then try tackling big projects. You'll appreciate Python more that way too. :)


Edit:

I personally suggest that, if you have free time, learn C# first instead. As soon as you get comfortable with it, start learning about the unsafe keyword and how to use pointers and do marshaling in C#. You'll still have a really big library to work with, like with Python, but you'll be able to do a lot of things that you can do in C. Then transition into calling system API's manually (like InternetOpen), and learn how to get those to work. You'll still have a lot of flexibility but you'll get a great introduction to lower-level concepts. After you've done these, then start learning C -- then you'll find it to be a piece of cake, and you'll see (C? get it?) the advantages and disadvantages of high-level and low-level languages, and you'll be able to find the one that suits your needs.

That said, I think A Crash Course in C might be useful as a starting point to jump into the sea C.

8

Don't read kernel code when you're learning C. It's full of tricks to squeeze every last bit of performance out of it. Some of it is very non-standard. It'll drive you crazy.

Instead, take a look at a few smallish, high code quality projects:

  • Lua - If you're not interested in programming languages, you may want to skip this one. Otherwise, it's truly beautiful code and amazing to see how much they accomplish with so little (something you seem frustrated by since you're coming from Python).
  • ZeroMQ - I love this project. It's a light-weight, super-fast message queue. Again, high quality stuff.
  • DarkHttpd - A 2700 line web server in C. Covers a few fun topics like socket programming, parsing, and working with protocols.
0

One purpose of a higher high level language is to hide complicated things. If you want to learn a "lower" level language, and don't want to start with really complicated things (that were formerly mostly hidden from you); try starting with some simpler learning projects, sorting an array of numbers, or reversing a string of characters, or some such...

It's easier to go up a steep hill by taking small steps.

-3

As far as cool projects to look at: Unix kernels are written in C.

1
  • Did you mean to add a link?
    – jmort253
    Feb 27, 2011 at 2:42
-3

Learn C grammar first , and then use it to write data structure and algorithm. Of course you can read Linux kernel as you expert c language.

Not the answer you're looking for? Browse other questions tagged or ask your own question.