Proper way to learn c ?

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

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: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.
Thread support, synchronization, time management are in the Standard C++ Library now.
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: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.
Thread support, synchronization, time management are in the Standard C++ Library now.
I doubt that this makes much difference in practice. C has pthreads (even on windows) which provides all of that functionality.

PS: I'm aware that the new C++ standard does include the threading stuff in it, I'm not sure if it's all already usable on current compilers though.
Daniel Shawul
Posts: 4185
Joined: Tue Mar 14, 2006 11:34 am
Location: Ethiopia

Re: Proper way to learn c ?

Post by Daniel Shawul »

rbarreira wrote:
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.
I agree that using c for chess engines is enough. But the OP already knows two OOP languages, so it is better for him to concentrate on improving algorithms than rewrite in c. If your ultimate goal is speed you can use assembly ofcourse, which is actually easy to learn but tedious to write. So the choice of C is not definite. It is even slower than FORTRAN so it is not the best "compromise language". My choice of programming languages go as follows Java-C++-FORTRAN/C-Assembly. Most AI aside from cut-throat chess programming is better done with Java :)
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 »

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.
The original post is about someone who wants to learn C, so we pointed out some of the best material for that.

But regarding your ill-advised notion that C is disguised assembly and C++ is a high-level language, maybe Linus Torvaldes can enlighten you. And before you use that argument: Linus knows C++ better than any of us, and he has at one point rewritten the kernel in C++, which helped him to understand why this language is a total abortion (he then rewrote it back in C, what a waste of time):
C++ is a horrible language. It's made more horrible by the fact that a lot
of substandard programmers use it, to the point where it's much much
easier to generate total and utter crap with it. Quite frankly, even if
the choice of C were to do *nothing* but keep the C++ programmers out,
that in itself would be a huge reason to use C.

In other words: the choice of C is the only sane choice. I know Miles
Bader jokingly said "to piss you off", but it's actually true. I've come
to the conclusion that any programmer that would prefer the project to be
in C++ over C is likely a programmer that I really *would* prefer to piss
off, so that he doesn't come and screw up any project I'm involved with.

C++ leads to really really bad design choices. You invariably start using
the "nice" library features of the language like STL and Boost and other
total and utter crap, that may "help" you program, but causes:

- infinite amounts of pain when they don't work (and anybody who tells me
that STL and especially Boost are stable and portable is just so full
of BS that it's not even funny)

- inefficient abstracted programming models where two years down the road
you notice that some abstraction wasn't very efficient, but now all
your code depends on all the nice object models around it, and you
cannot fix it without rewriting your app.

In other words, the only way to do good, efficient, and system-level and
portable C++ ends up to limit yourself to all the things that are
basically available in C. And limiting your project to C means that people
don't screw that up, and also means that you get a lot of programmers that
do actually understand low-level issues and don't screw things up with any
idiotic "object model" crap.
abulmo
Posts: 151
Joined: Thu Nov 12, 2009 6:31 pm

Re: Proper way to learn c ?

Post by abulmo »

Aleks Peshkov wrote:
rbarreira wrote: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.
Thread support, synchronization, time management are in the Standard C++ Library now.
C11 (the last C standard) supports this too.
Richard
jdart
Posts: 4367
Joined: Fri Mar 10, 2006 5:23 am
Location: http://www.arasanchess.org

Re: C++ vs. C

Post by jdart »

Bjarne Stroustrup: "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off."

Which is a clever way of saying, if you use it right, you get some improved safety and productivity vs. C; if you use it wrong, you get neither.

Performance should be virtually the same, again, if you know what you are doing.

Portability issues and bugginess used to be an issue, but after a couple of decades, compiler writers have found out how to implement the language, mostly :-).

That said, I still wouldn't use it to write a kernel.

--Jon
Daniel Shawul
Posts: 4185
Joined: Tue Mar 14, 2006 11:34 am
Location: Ethiopia

Re: Proper way to learn c ?

Post by Daniel Shawul »

The original post is about someone who wants to learn C, so we pointed out some of the best material for that.
Yes and that someone also knows two OOP languages. This is not another this language is better debate. Infact you will have more knowlege about optimization if you work your way from a high-level language not some pseudo-assembly like C. Get it?
But regarding your ill-advised notion that C is disguised assembly and C++ is a high-level language, maybe Linus Torvaldes can enlighten you. And before you use that argument: Linus knows C++ better than any of us, and he has at one point rewritten the kernel in C++, which helped him to understand why this language is a total abortion (he then rewrote it back in C, what a waste of time):
Calm down. Your opinions seem to come from worshiping what someone says without even considering why he said it. Ofcourse he is not going to say C++ is better when he is already written his kernel with C 8-) But you missed the whole point of my argument. Why go low level specially when the OP knows two OOP languages ? Why don't you write your program with FORTRAN which has no pointers and will definately be faster ? And better yet why not assembly?
mar
Posts: 2559
Joined: Fri Nov 26, 2010 2:00 pm
Location: Czech Republic
Full name: Martin Sedlak

Re: Proper way to learn c ?

Post by mar »

Huh. C++ is a superset of C. So I really don't see why Torvalds wanted to rewrite the kernel in C++,
that's completely stupid as it would break existing C programs. :shock:
Daniel Shawul
Posts: 4185
Joined: Tue Mar 14, 2006 11:34 am
Location: Ethiopia

Re: Proper way to learn c ?

Post by Daniel Shawul »

mar wrote:Huh. C++ is a superset of C. So I really don't see why Torvalds wanted to rewrite the kernel in C++,
that's completely stupid as it would break existing C programs. :shock:
He was just angry some people perfered to use c++ in git. Dr Dobb's on linus' rant. http://www.drdobbs.com/cpp/linus-and-c/229700143
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 »

mar wrote:Huh. C++ is a superset of C. So I really don't see why Torvalds wanted to rewrite the kernel in C++,
that's completely stupid as it would break existing C programs. :shock:
I see two reasons why that "C++ rewrite" of the Linux kernel failed:

1) You cannot simply "rewrite" something as complex as an OS kernel in C++ unless you also redesign it. A procedural-style software of that magnitude will remain procedural even if you introduce some classes, inheritance, something here and something there. I don't say that Linus made no attempt of a redesign but at least "rewriting in C++" does not suggest to me he did so.

2) In many cases the best programming language for a given task is the one that a developer likes most, and is most experienced in.

Sven