Proper way to learn c ?

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

schafro

Proper way to learn c ?

Post by schafro »

Hi I've started programing about 5 months ago , I have some experience with OOP languages like Java and Obj-C but I lack the experience when it comes to using procedural languages for medium-large sized projects. I'm looking for some pointers like books, resources, videos for learning C. And I was hopping that some of the more experience chess programmers can tell me on what part of the language I should focus the most when it comes to chess programing. I'm planing at some point to start coding my own chess engine just as a learning exercise.So any help is welcomed.Thanks in advance for your time spent educating me.
Dan Andersson
Posts: 442
Joined: Wed Mar 08, 2006 8:54 pm

Re: Proper way to learn c ?

Post by Dan Andersson »

I'd say reading the K&R book and working through a decent practical example like 'Let's build a compiler'. The book is short but informative and the project is the construction of a compiler for a subset of C. It's certainly doable in a week or two.

MvH Dan Andersson
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: Proper way to learn c ?

Post by Sven »

Hi Daniel,

I can't help you with resources like books or videos (I learned C from K&R - "Kernighan & Ritchie" - at university 26 years ago; an excellent book of course but the language standard has changed since then so you'd need something more recent in the meantime, at least in addition to K&R C which is still good for initial learning).

But when trying to take your viewpoint as an OO developer who wants to learn C, I think you might need to get familiar at least with

- the typing system in C and its pitfalls, as that is where pure OO languages and their compilers are often much better than C, and as you need to know how explicit and implicit type conversions work and how dangerous type casts can be sometimes (while another time they may be necessary);

- structs (as that is how you can represent the concept of an object's data in C);

- pointers (as that is how you often pass an object's data to a function in C, while Java for instance lacks pointers AFAIK but uses pointers resp. references internally as well when passing objects as parameters);

- various issues of low-level memory layout, size of data types, structure alignment, etc., as that is what languages like Java or Obj-C almost completely hide to you while C sometimes requires to deal with those issues;

- some typical standard library functions which are frequently used (and can appear also in a chess program), like printf(), memset(), strlen(), strcpy(), rand(), fgets() etc.;

- the C preprocessor, as definitely all serious books suggest to avoid using C preprocessor macros (which is right in general) but you still need to understand the meaning and use of things like #define and #ifdef to understand C programs and how the C compiler works;

- especially the preprocessor directive #include, as that is the recommended - even though ugly - way to import "interfaces" (no, there is no such thing in C in reality, only on the conceptual level);

- and perhaps finally some typical ways to decompose your C program into modules such that it remotely resembles an "OO" program, for instance by grouping the implementation of all functions that operate on one user-defined data type (struct) in one .c module while putting the "interface" declarations of all those functions in the corresponding .h file (as far as they are used outside).

That's for a start, there may be much more of course :-)

Sven
User avatar
lucasart
Posts: 3232
Joined: Mon May 31, 2010 1:29 pm
Full name: lucasart

Re: Proper way to learn c ?

Post by lucasart »

schafro wrote:Hi I've started programing about 5 months ago , I have some experience with OOP languages like Java and Obj-C but I lack the experience when it comes to using procedural languages for medium-large sized projects. I'm looking for some pointers like books, resources, videos for learning C. And I was hopping that some of the more experience chess programmers can tell me on what part of the language I should focus the most when it comes to chess programing. I'm planing at some point to start coding my own chess engine just as a learning exercise.So any help is welcomed.Thanks in advance for your time spent educating me.
The K&R is the best book I know on C. Free PDF download:
http://www.google.com.hk/url?sa=t&rct=j ... ug&cad=rja

You should read it thouroughlly, and do the exercises (there are plenty of examples and exercises after every chapter). Basically you know programming in general (ie. algorithmics) but you need to unlearn all the Java and Objective-C heresy. The hardest thing to grasp for an OO programmer might be pointers, and string manipulations. But you'll see, C is a very simple and natural language: it's efficiency lies in the fact that what ou write in C follows in a much more real way what the machine is doing.
Aleks Peshkov
Posts: 892
Joined: Sun Nov 19, 2006 9:16 pm
Location: Russia

Re: Proper way to learn c ?

Post by Aleks Peshkov »

User avatar
lucasart
Posts: 3232
Joined: Mon May 31, 2010 1:29 pm
Full name: lucasart

Re: Proper way to learn c ?

Post by lucasart »

Aleks Peshkov wrote:I suggest to view http://www2.research.att.com/~bs/programming.html
That's C++. It is quite different from C, and a lot more complex to master IMO.
Daniel Shawul
Posts: 4185
Joined: Tue Mar 14, 2006 11:34 am
Location: Ethiopia

Re: Proper way to learn c ?

Post by Daniel Shawul »

I suggest you stick with the high level languages since those are more productive and will keep you focused to what you want to do. C is disguised assembly that calls itself a high level language. There is no reason why c would give you more insight than c++ for optimization reasons, but it does lack so many libraries that you may spend too much time reinventing the wheel.
rbarreira
Posts: 900
Joined: Tue Apr 27, 2010 3:48 pm

Re: Proper way to learn c ?

Post by rbarreira »

Daniel Shawul wrote:I suggest you stick with the high level languages since those are more productive and will keep you focused to what you want to do. C is disguised assembly that calls itself a high level language. There is no reason why c would give you more insight than c++ for optimization reasons, but it does lack so many libraries that you may spend too much time reinventing the wheel.
Depends on what kind of program you want to develop. If we're talking about chess engines, you don't really need fancy libraries. All the data structures you're likely to need are hash tables, linked lists and maybe priority queues. You can implement all of those combined in 100-200 lines of C code if you need to, so you won't spend much time at all reinventing the wheel. As a benefit you'll end up with exactly the implementation you need, and not a generic one...

As for being productive, I've found that some C++ programmers spend more time reading FAQs on obscure language complexities than actually coding, so it's not all roses. Also, remember than even if it's true that a language makes you code twice as fast, that doesn't make you twice as effective as a programmer. A lot more time is spent on design and thinking about the problems at hand than on actual coding.

For programs without terribly complex data structures, C lets you focus on the task at hand and doesn't get in the way.
Aleks Peshkov
Posts: 892
Joined: Sun Nov 19, 2006 9:16 pm
Location: Russia

Re: Proper way to learn c ?

Post by Aleks Peshkov »

rbarreira wrote:If we're talking about chess engines, you don't really need fancy libraries. All the data structures you're likely to need are hash tables, linked lists and maybe priority queues. You can implement all of those combined in 100-200 lines of C code if you need to, so you won't spend much time at all reinventing the wheel.
What about multithreading?
rbarreira
Posts: 900
Joined: Tue Apr 27, 2010 3:48 pm

Re: Proper way to learn c ?

Post by rbarreira »

Aleks Peshkov wrote:
rbarreira wrote:If we're talking about chess engines, you don't really need fancy libraries. All the data structures you're likely to need are hash tables, linked lists and maybe priority queues. You can implement all of those combined in 100-200 lines of C code if you need to, so you won't spend much time at all reinventing the wheel.
What about multithreading?
On most operating systems (and all of the ones that are common), C does have threading libraries. Not in the language itself but in the OS's libraries. I don't think there's a big difference between C and C++ in this regard.