You are not logged in.

#1 2011-10-22 23:47:12

brandon88tube
Member
Registered: 2009-11-06
Posts: 84

Learning C question

I've been trying to learn C on the side and wanted to know if anyone could point me to a good place to go to help me learn more or a good book. Unfortunately, I'm not really the type that is able to read something and just understand it. I think I'm more of a visual learner or one that learns best if I have a bunch of good examples. So, most of the C tutorials I've found are not that helpful to me. I'm able to understand some of it as I do some programming in other languages for work, but they are all high-level and a lot easier to use. As practice I've started to create a very basic window manager and have been using tinywm and dwm as reference, but dwm is way too advanced for me at this point and I get really lost. I might be asking in the wrong place, but I figured it couldn't hurt to just throw it out there and maybe I'd get lucky and find some help that I could understand.

Offline

#2 2011-10-23 09:44:48

ibrunton
Member
From: Canada
Registered: 2011-05-05
Posts: 270

Re: Learning C question

The C Programming Language by Brian Kernighan and Dennis Ritchie is the classic and full of good examples.

Offline

#3 2011-10-23 11:23:00

x33a
Forum Fellow
Registered: 2009-08-15
Posts: 4,587

Re: Learning C question

Have you taken a look at this website:

http://www.cprogramming.com/index.html

Offline

#4 2011-10-23 11:30:23

stqn
Member
Registered: 2010-03-19
Posts: 1,191
Website

Re: Learning C question

I'm sure there are other threads like this but since a search for "learn c" didn't return anything relevant, I'll mention http://projecteuler.net/ . It's not a site to learn C, but there are 350+ mathematical problems that can be solved with very small programs, and (together with a book) that can help you learn parts of any language by practice. You won't learn much about pointers that way, though; mostly loops, arrays and recursion.

Offline

#5 2011-10-23 15:13:42

brandon88tube
Member
Registered: 2009-11-06
Posts: 84

Re: Learning C question

I understand arrays, loops and the other basic things like if statements because of my other programming knowledge, but what tends to get me is the whole pointer thing and memory management. I don't understand the point of using a pointer over a normal variable. I might already be using them at work, but unknowingly so, as I make variables that sort of reference something else. Either way, the way they are used seems to confuse me for some odd reason and I just can't grasp the concept. Then there is the whole memory thing... Never had to do this before so never sure when and where it would be needed.

Offline

#6 2011-10-23 15:30:36

JHeaton
Member
From: United Kingdom
Registered: 2009-05-16
Posts: 158

Re: Learning C question

Ah, pointers. It took me forever to start understanding them, and even now I have trouble. This guide helped massively: http://pw1.netcom.com/~tjensen/ptr/pointers.htm

Offline

#7 2011-10-25 20:54:24

Basu
Member
From: Cornell University
Registered: 2006-12-15
Posts: 296
Website

Re: Learning C question

Learn C the Hard Way is a work in progress. But if the Python equivalent is anything to go by, it should be awesome once it gets out.


The Bytebaker -- Computer science is not a science and it's not about computers
Check out my open source software at Github

Offline

#8 2011-10-25 23:51:53

Grinch
Member
Registered: 2010-11-07
Posts: 265

Re: Learning C question

Basu wrote:

Learn C the Hard Way is a work in progress. But if the Python equivalent is anything to go by, it should be awesome once it gets out.

I persused the chapters available and I must say I like his approach, I wish I had something like this back when I was learning C, very hands on. Hard way FTW!

Offline

#9 2011-10-26 00:22:43

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 19,728

Re: Learning C question

I suggest The C Puzzle Book by Alan R. Feuer

It is a thought book in which you solve riddles about C in your head or on paper.  It drove many a concept home for me.  Want to understand pointers, see the puzzle called "Pointer Stew"

edit: BTW it can be read on-line from many sources.

Last edited by ewaller (2011-10-26 00:23:20)


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#10 2011-10-26 00:55:16

/dev/zero
Member
From: Melbourne, Australia
Registered: 2011-10-20
Posts: 1,247

Re: Learning C question

brandon88tube wrote:

I don't understand the point of using a pointer over a normal variable.

I worked on a project where we did traffic simulations. We programmed in C++, but in the general style of "C with classes" :-P

We had various objects representing things like roads, cars, and traffic lights. Passing these objects around as normal variables would be either impossible or very costly.

As part of this project, I gained an intuition for pointers which goes a little like this: instead of lugging a dead cow to your butchers knife, take your knife to the cow. IOW, with a pointer, you sort of reach in and manipulate something remotely, without moving it around. This makes everything a lot faster. The only problem is if you grab your knife and forget where the cow is, the cow will start to smell after a while.

Edit: actually a better analogy that just came to mind goes like this. When you want your friends to visit your house, you don't pick your house up and take it to your friends. You just sms them your address. And the address doesn't even need to be in human-readable form, provided the gps on their phone knows what you're talking about.

Last edited by /dev/zero (2011-10-26 01:13:43)

Offline

#11 2011-10-26 01:23:45

keenerd
Package Maintainer (PM)
Registered: 2007-02-22
Posts: 647
Website

Re: Learning C question

/dev/zero wrote:

instead of lugging a dead cow to your butchers knife, take your knife to the cow.

This is quite enlightening.  I'd really like to hear your description of a monad or combinator.

Offline

#12 2011-10-26 01:55:56

/dev/zero
Member
From: Melbourne, Australia
Registered: 2011-10-20
Posts: 1,247

Re: Learning C question

keenerd wrote:

I'd really like to hear your description of a monad or combinator.

lol. I haven't used monads or combinators, so it's hard to say :-)

At first glance, monads kind of seem to me quite similar to the unixy idea of a pipeline, correct? - which would imply a plumbing analogy.

For combinators, I gather they provide a variant on the lambda calculus, itself equivalent to the Turing machine. The Turing machine then furnishes us with analogies that concretise "computation".

Edit: but this is getting off topic. Sorry OP!!

Last edited by /dev/zero (2011-10-26 02:01:39)

Offline

#13 2011-10-26 02:03:52

Grinch
Member
Registered: 2010-11-07
Posts: 265

Re: Learning C question

brandon88tube wrote:

I understand arrays, loops and the other basic things like if statements because of my other programming knowledge, but what tends to get me is the whole pointer thing and memory management. I don't understand the point of using a pointer over a normal variable. I might already be using them at work, but unknowingly so, as I make variables that sort of reference something else. Either way, the way they are used seems to confuse me for some odd reason and I just can't grasp the concept. Then there is the whole memory thing... Never had to do this before so never sure when and where it would be needed.

brandon88tube wrote:

I understand arrays, loops and the other basic things like if statements because of my other programming knowledge, but what tends to get me is the whole pointer thing and memory management. I don't understand the point of using a pointer over a normal variable. I might already be using them at work, but unknowingly so, as I make variables that sort of reference something else. Either way, the way they are used seems to confuse me for some odd reason and I just can't grasp the concept. Then there is the whole memory thing... Never had to do this before so never sure when and where it would be needed.

Well, I think the whole thing is that you don't fully grasp the memory bit, if you understand that and what a memory address is then pointers all fall to place.

Think of the computer's memory as a huge array of cells which each can hold 0 or 1. In order to access these cells more efficiently the cpu groups them into larger chunks and allow you fast access to the memory using these chunks. For example a 32-bit cpu it can take a chunk of 32-bits at a time and manipulate those bits and/or move them to another memory location (cell chunk).  Say we want to send a 32-bit value to a function, no problem. 32-bits can be moved the the functions local memory (stack) extremely quickly. However, what if we want to send a huge chunk of data to a function? It doesn't fit into 32-bits and will require the cpu to do alot of work in copying this large data to the function's stack in repeated 32-bit chunks which is slow. But why are we copying this data at all? The data already exist somewhere, can't we just tell the function where it is rather than copying all that data to the function? Sure, that is what a pointer does. It simply points at a chunk of data in memory and the type the pointer has tells whatever code that uses it how that data is to be accessed.

So, for smaller values which fit into what the the cpu can handle in one chunk (8-bit/32-bit/64-bit etc based on cpu) you generally want to copy the value to the function since it's not slower than passing a pointer to it and allows the function to use the value locally which helps with optimization. However with larger data structures it's not a good idea to pass and return them to/from a function since that is VERY slow, better to tell the function where the data is and to work directly on that, in other words a 'pointer' to that data.

Hmmm... yes this is very simplified but it may help you grasp part of what makes pointers useful and why you'd want to pass pointers rather than data when calling functions. Let me know if there's something you didn't understand from my ramblings here wink

Offline

#14 2011-10-26 10:31:55

[DoodoM]
Member
From: Bilbao/Spain
Registered: 2008-04-25
Posts: 13

Re: Learning C question

I think this is a very good site to start learning:
http://c.learncodethehardway.org/book/

I also have bought myself the K&R book on Amazon.

Offline

#15 2011-10-28 10:00:33

trontonic
Member
Registered: 2008-07-21
Posts: 80

Re: Learning C question

I second Learn C the hard way. Have you compiled your first C program? If you wish, I (or we, I'm sure others would join in) could write a short post for how to get started with "hello world" in Arch?

Offline

#16 2011-10-28 10:47:39

[DoodoM]
Member
From: Bilbao/Spain
Registered: 2008-04-25
Posts: 13

Re: Learning C question

I'm currently programming on my Mac Mini, but yes, I would. But I have the problem that I am not english fluent, and it's harder to me to make myself understood in this language.

Offline

#17 2011-10-28 11:23:26

trontonic
Member
Registered: 2008-07-21
Posts: 80

Re: Learning C question

If you write simple sentences in Google Translate, it may work out? smile

Offline

#18 2011-10-28 11:48:32

[DoodoM]
Member
From: Bilbao/Spain
Registered: 2008-04-25
Posts: 13

Re: Learning C question

Thanks, but I prefer to think in english and not to think in my language and let an awful translation engine to translate my thoughts. tongue

Although I have to say I'm quite newbie with C (In fact, I have more experience with PHP). I don't know what I could help teach to others about programming that they don't know themselves. It would be great for people who has not programmed yet, but not for people who has already programmed in other languages.

Last edited by [DoodoM] (2011-10-28 11:54:04)

Offline

#19 2011-10-28 11:58:37

trontonic
Member
Registered: 2008-07-21
Posts: 80

Re: Learning C question

For the record, I think your English is just fine. smile

Offline

#20 2011-10-28 19:42:55

Basu
Member
From: Cornell University
Registered: 2006-12-15
Posts: 296
Website

Re: Learning C question

[DoodoM] wrote:

Thanks, but I prefer to think in english and not to think in my language and let an awful translation engine to translate my thoughts. tongue

Although I have to say I'm quite newbie with C (In fact, I have more experience with PHP). I don't know what I could help teach to others about programming that they don't know themselves. It would be great for people who has not programmed yet, but not for people who has already programmed in other languages.

Most things improve with practice, especially when it comes to languages (whether it be English, C, mathematics or music). We were all newbies once upon a time (and I, for one, still am) but we try to suck a little less each day and the days build up.


The Bytebaker -- Computer science is not a science and it's not about computers
Check out my open source software at Github

Offline

#21 2011-10-30 02:47:19

brandon88tube
Member
Registered: 2009-11-06
Posts: 84

Re: Learning C question

I appreciate the help that has been posted thus far and would love to see some continuation as it seems a few others may be interested as well. I also don't mind the analogies getting a little off topic and it was related to C in a sense that it dealt with programming. Heck, I never even heard of monad and combinator up until now. I plan on checking out a few of the links/books that were posted, but I'll admit that this will probably be a slow learning process for me. Anyways, thanks again everyone and please feel free to share some more knowledge.

Offline

#22 2011-10-30 02:53:56

/dev/zero
Member
From: Melbourne, Australia
Registered: 2011-10-20
Posts: 1,247

Re: Learning C question

@Mods: Would it be worth stickying this topic? As brandon88tube says, I think this is one of those things lots of people have an ongoing interest in.

I know I find it interesting and could definitely do with more skill in C as opposed to so-called higher level (i.e. bloated, noob) languages.

Offline

#23 2011-10-30 04:10:37

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: Learning C question

/dev/zero wrote:

@Mods: Would it be worth stickying this topic?

What about this one, or this one, or even this one?

This comes up from time to time; there is no real need to sticky it smile


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#24 2011-10-30 04:23:26

brandon88tube
Member
Registered: 2009-11-06
Posts: 84

Re: Learning C question

What I think should be made and put up as a sticky is a post on learning resources for different languages that people can contribute to.

Example:

Language = C;
Resources =[
bookA,
bookB,
website1,
website2
];

Language = Python;
Resources =[
siteC,
book3
];

Last edited by brandon88tube (2011-10-30 04:26:11)

Offline

#25 2011-10-30 04:24:25

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: Learning C question

Good idea: but it sounds more like something for the wiki


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

Board footer

Powered by FluxBB