1

I have a college degree in CS, and learned about Java and OCaml. Java was used as introduction, and in most CS classed, except the theoretical ones. OCaml was part of learning a different programming paradigm. Since OCaml wasn't a successful entry in my CV, I became a better Java programmer. Also, most jobs are for Java programmers. Later on I learned Golang.

I know about the simplifications provided by Java, i.e. garbage collection. I also know, that C doesn't do garbage collection and you have to know when to allocate and when to free the memory. But during my education I was never taught to think in this kind of way. We never learned algorithms in the aspect of memory. I feel like I missed something.

So my question is, how deep do I have to dig, before I can start writing passable C code, without having myself put obstacles in my way, because of my own crufty code and deep misunderstanding of relevant issues?

  • There's nothing you can do to make your C code good right off the bat, except perhaps learning a full language that's very C-like (of which there are very few). Don't be afraid to write crappy code, just learn why it's crappy. Your early Java programs were bad, and now you're good at Java, right? – user7043 Jul 26 '14 at 17:31
  • 2
    You should know that C will not protect you from your mistakes. – Blrfl Jul 26 '14 at 18:48
4

There is a very long answer above, I will give a short one.

I started in Java too and then started c for a good base for the rest of my programming career.

It doesn't take too long to start writing good code. Luckily I had a professor to help me.

Most important

Pointers pointers pointers

**allocating and freeing up memory **

After you get a good grasp of those concepts you should be able to take off running.

| improve this answer | |
3

If you haven't done so already, read "The C Programming Language" and work through the examples. I learned C by way of this book (coming from FORTRAN). Since you are already aware of the issues and pitfalls of C-style memory management, you're already off to a good start.

A programming principal you should always follow is to never leave a "dangling" pointer (a pointer which refers to de-allocated memory) - always initialize pointers to null, and when memory is freed, always set the associated pointer to null. A check for null can then be used as a test for whether memory has been allocated.

To make it easier to avoid dangling pointers, another principal is symmetry. In object-oriented programming (C++, for example) it is expressed as: he who allocates memory is responsible for freeing it. This is typically implemented in classes such that each method which "new"s an object has a counterpart method in the same class responsible for "delete"ing that which was "new"d.

In C, the principle is obeyed by implementing function pairs where allocated memory is used; for each function which performs allocation(s), there should be another function which performs "cleanup" - freeing the allocated memory.

| improve this answer | |
  • That book is outated. – toasted_flakes Jul 26 '14 at 21:01
  • @toasted_flakes while its not C11, unless you find a first edition copy in a used book store... the current edition deals with ANSI C (C89) and is a very reasonable and accessible book to learn C from. Its not the '78 flavor of C. – user40980 Jul 27 '14 at 23:46
2

Passable code in any language demonstrates know-how in how the language can be used to write readable code in the large and small. Which of course takes practice! Specifically I'd expect you to know how to:

  1. Break down code into modular chunks
  2. Write code that's idiomatic for that language
  3. The standard library for this language
  4. Know what libraries exist for common tasks
  5. Best practices for writing libraries
  6. Build systems if applicable
  7. How to do automated testing of your code

Specifically for C, I'd expect knowledge of:

  1. Pointers
  2. How an array is a pointer is an array (sort of)
  3. How a C string is a special kind of array that needs special care
  4. Manual Memory allocation (and most importantly deallocation) -- and idioms for management thereof
  5. How the stack works vs how the heap works (value vs reference based semantics)
  6. Comfort building/using your own data structures
  7. Probably comfort with the platform you're writing on (posix, or whatever syscalls you care about)

But you want be able to do any of this unless you write really bad code for a really long time and make lots of mistakes! See also Norvig: Teach Yourself Programming in Ten Years.

| improve this answer | |
0

I think learn C is a good idea, because many language is based on C, understand how memory works can be only good, and sometimes make C for solve problem can be useful.

The good first way you can make, it's read the C programming language, some give very solid knowledge. After you can find a cool free software write in C and contribute.

| improve this answer | |

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