tags:

views:

100

answers:

5

I want to broaden my programming experience and so figured that learning C (not C++) would be a nice idea - I've got a starting project (some simple 2D games using SDL) and given it a go, but I have to admit I'm struggling.

Now I consider myself to be a very a competent C# developer, but C is just a completely different world!

  • No namespaces.
  • Without classes I don't know how to structure my code.
  • I understand header files & includes, but my use of them doesn't seem quite right.
  • The loss of (for example) the String class is a bit of a culture shock.

On top of that I'm finding it tricky to separate out the C++ information from the C information (for example is snprintf a C++ only thing?)

This may sound like an odd request, but are there any good guides for learning C if you have experience in other languages?

+1  A: 

I'd recommend you start with a book on C. It's not a good idea to learn a new language in constant comparison with a previous one, you end up learning what's different and not what's new and unique.

Take a look at Kernighan and Ritchie's book, or Deitel & Deitel's. They will both strike you as overly basic at first, but you'll end up with a much more solid knowledge of the language.

edit: a friend of mine right here suggests you also find something on data structures and algorithms after you've grasped the language, since you'll be working in an environment with no objects.

Hope it helps!

Santiago Lezica
+3  A: 

Don't try to learn C from C++ books/web sites. Start with something aimed only at C. A great place is The C Programming Language. It's the original and still one of the best programming language books out there.

Learning C is going to be a struggle because it's a different kind of language. It's a procedural language, not an object oriented language. It will be good to stretch your brain in learning it. You'll likely also learn a lot more about the way your OS works because it's down much closer to the metal.

Note also that it's a much less complex language and thus is missing a lot of the niceties of a modern language. There is no BCL/.Net Framework. As you noticed, there is no string class (although there are string functions).

Steve Rowe
+1 for mentioning the tome. I think *everyone* regardless of what language they're using should read that book. It's one of the best programming books ever written.
Noufal Ibrahim
A: 

I'll answer you question, but first: Going from C# to C is like going from a plastic toothbrush to a wooden one... I see no advantage.

So:

Namespaces - No, there aren't any in C. This is a C++/C# feature. You should be able to compensate with prefixes. (namespace_myClass).

As for classes, I don't know how to help you. I am a C++ programmer (not C), but C does have struct, though I don't know if you can have member functions.

Header files and includes are syntactically the same. For example:

#include <Windows.h>

gives you windows functionality. Note that, you may need to link in libraries for many header files, like openGL.

And what will we do without string? You could make your own, or you can manually manage C strings (char*) with the C functions such memcpy, ect...

Farewell and goodluck!

Alexander Rafferty
C# and C have different strengths. C is still mostly the preferred tool for embedded work. There are *genuine* advantages from trying C out.
Noufal Ibrahim
A: 

C++ is a superset of C. So anything you can do in C, you can do in C++.

I think it is a good idea to learn C. It will make you have to deal with memory and resource management, structured programming vs object-oriented, a it has a much smaller standard library as well (compared to C#).

KSchmidt
C++ isn't *technically* (in the mathematical sense) a superset of C, and in the computational sense (if you meant it that way), all turing-complete languages are equivalent. It's important to make this distinction because some C code won't compile when using a C++ compiler, and (obviously) C++ code won't compile with a C compiler.
David Titarenco
I was over-simplifying: http://public.research.att.com/~bs/bs_faq.html#C-is-subset
KSchmidt