chrisphil00
Posts: 14
Joined: Wed Apr 03, 2013 12:07 am

How do I learn C

Fri Jan 30, 2015 1:31 am

I want to learn C. What software or program do I use to learn C? Do I use Geany to learn how to program in C? If I am susposed to use Geany, how do I use Geany? Any help would be great. Thank you in advanced!

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: Korea

Re: How do I learn C

Fri Jan 30, 2015 2:21 am

You need a text editor and a compiler.

Any text editor will do. Use gcc for your compiler.

You can learn C (or any language) in two ways.

Either:
Think of a small problem, then write a program to solve it. Use Google to find out the bits you need as you go along.

Or:
Find a beginner's tutorial online, or a book, and follow the exercises. This will expose you to the whole language, but you might never use some parts of it.

Most programmers only ever write one program. They just modify it each time they need it to do something else.

User avatar
buja
Posts: 498
Joined: Wed Dec 31, 2014 8:21 am
Location: Netherlands

Re: How do I learn C

Fri Jan 30, 2015 6:45 am

How do you learn any programming language?
I would say buy a book, read it and make the exercises. And of course, have your own experiments as soon as possible.

Books I can recommend on C are (pick one):
- Programming in C, by Stephen G. Kochan
- C Programming, A Modern Approach, by K.N. King

I think these books are better than (most) on-line resources.

Raspbian, as any Linux system, comes with gcc, an excellent C-compiler.
And Geany is an excellent editor/IDE and very easy to use, certainly for the many small exercises while you are learning C (or any programming language).

User avatar
Redrobes
Posts: 80
Joined: Mon Dec 26, 2011 9:19 pm
Location: S.W. UK
Contact: Website

Re: How do I learn C

Fri Jan 30, 2015 12:21 pm

You need a raspberry pi running and ideally it boots into a shell or you start a terminal shell from the GUI.

Then you need any text editor. You can use geany with the full GUI / IDE etc but I think doing a small program with just a text editor strips away all the confusing fluff and leaves you with the raw process. I think 'nano' as a basic text editor is a good starting one. Some people use 'vi' but it has some confusing things about it when starting out. All you need to make your first program from your text editor is to be able to type new code, edit existing lines and then save it.

GCC is your compiler and its built into the raspberry pi distro so nothing to do there to get it.

You start a new text file like 'hello.c'

Then you put in your C code into that text file using your text editor. Then you compile it. The result is an executable file you can run which does whatever you asked it to do. To compile it you type something like this:

gcc hello.c -o hello

You should get a new file called 'hello' which is executable if it compiled correctly.

My recommendation on a book is the kernighan and ritchie one. I mean its a classic. Look at the date on it and how many stars its rated at. You cant go wrong with this one. Also K&R is known as the C bible because those two guys invented C. Its also well written and an easy read.
http://www.amazon.co.uk/C-Programming-L ... ref=sr_1_1

chrisphil00
Posts: 14
Joined: Wed Apr 03, 2013 12:07 am

Re: How do I learn C

Fri Jan 30, 2015 2:31 pm

Thank you all very much! :D All my questions were answered, this has helped a lot!

Ravenous
Posts: 1956
Joined: Fri Feb 24, 2012 1:01 pm
Location: UK

Re: How do I learn C

Fri Jan 30, 2015 2:36 pm

Don't forget the classic Gotcha:

to run your compiled code "hello" might not work. If not try "./hello" instead (it tells the operating system to look explicitly in your current directory for the program. I'm not sure why...)

morgon
Posts: 68
Joined: Sun Dec 22, 2013 1:10 pm

Re: How do I learn C

Fri Jan 30, 2015 2:39 pm

K&R is of course a classic but also somewhat dated.

If you already have some programming experience the brand new 2nd edition of "21st Century C" from OReilly may be for you.

Joe Schmoe
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm

Re: How do I learn C

Fri Jan 30, 2015 2:48 pm

Ravenous wrote:Don't forget the classic Gotcha:

to run your compiled code "hello" might not work. If not try "./hello" instead (it tells the operating system to look explicitly in your current directory for the program. I'm not sure why...)
Actually, just "hello" should work, but you may need to install it. It is not installed by default.
After I did "apt-get install hello", it ran as expected.

However, as you've noted, in Unix/Linux, the current directory is not in your PATH by default (and this is a Good Thing; do not try to change this!). And, unlike in Windows, the current directory is not automatically searched (and searched first, before anything else) - regardless of whether or not it is in the PATH.

Anyway, you will soon get used to the "./" thing. Actually, the "hello" problem is the mild form of it (assuming you haven't installed the system "hello" program, as described above). The killer version is when you name your program "test" and then run it as "test" - it may take a while before you figure out that it is running the system "test" program, totally ignoring your program. When I first got bit by this, I started naming my programs things like "t67" instead of "test" …
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)

morgon
Posts: 68
Joined: Sun Dec 22, 2013 1:10 pm

Re: How do I learn C

Fri Jan 30, 2015 2:52 pm

the current directory is not in your PATH by default (and this is a Good Thing; do not try to change this!).
That is a matter of taste. Don't do it for root. I usually have "." in my PATH for my normal login for convenience.
And, unlike in Windows, the current directory is not automatically searched (and searched first, before anything else) - regardless of whether or not it is in the PATH.
Not true. When "." is in PATH it is searched. When it is the first entry in PATH it is search first.

Joe Schmoe
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm

Re: How do I learn C

Fri Jan 30, 2015 3:01 pm

Not true. When "." is in PATH it is searched. When it is the first entry in PATH it is search first.
I had a little trouble expressing what I meant in the section you responded to. Think of it as a parenthesization problem.

What I meant to convey is that in Windows, the current directory is always searched (and searched first) - regardless of whether or not "." is in the PATH.

Obviously, in Unix/Linux, this comment does not apply.
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)

Ravenous
Posts: 1956
Joined: Fri Feb 24, 2012 1:01 pm
Location: UK

Re: How do I learn C

Fri Jan 30, 2015 3:11 pm

Interesting stuff about the path, guys (oh and yes, I did understand your first comment on the difference with Windows. It was one of the first things I tripped up on when I tried my first Linux in fact!)

User avatar
rpdom
Posts: 14770
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: How do I learn C

Fri Jan 30, 2015 5:55 pm

One of the first things I do with a (non-root) login on a new system is to create a ~/bin directory. Then that directory gets automatically added to the end of the PATH at log on time.

I put my finished code in that directory.

I usually run test code using the ./mycode method and am so used to that it is second nature. That and calling test programs "fred" (or sometimes "jim" and occasionally "sheila"). :)

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4258
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: How do I learn C

Fri Jan 30, 2015 6:03 pm

Joe Schmoe wrote:in Windows, the current directory is always searched (and searched first) - regardless of whether or not "." is in the PATH.
Interestingly, this is totally obsolete in Windows, where 99.9% of programs are started by clicking and 99% require installing in the registry and GAC. It does serve some sort of purpose in Linux, where coding a quick and dirty program is often the right thing to do.

So they've both got it wrong. ;)

User avatar
experix
Posts: 204
Joined: Mon Nov 10, 2014 7:39 pm
Location: Coquille OR
Contact: Website

Re: How do I learn C

Fri Jan 30, 2015 6:57 pm

When you have a little competence with the C language, the best way I know about to get proficient at that and learn the GNU-Linux system at the same time is by working through The Linux Programming Interface by Michael Kerrisk (http://man7.org/tlpi). Its lucid prose is a real pleasure to read, and the example programs will teach you good programming practices, useful techniques and the system and library interfaces that you will be dealing with at the same time.

User avatar
Douglas6
Posts: 4715
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: How do I learn C

Fri Jan 30, 2015 7:35 pm

rpdom wrote:That and calling test programs "fred" (or sometimes "jim" and occasionally "sheila"). :)
Ha. I've been calling my test programs 'fred' my whole life. Easy to type with just the left hand. But if I need a second one, it's always 'ethyl'.

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: Korea

Re: How do I learn C

Fri Jan 30, 2015 7:38 pm

Douglas6 wrote:
rpdom wrote:That and calling test programs "fred" (or sometimes "jim" and occasionally "sheila"). :)
Ha. I've been calling my test programs 'fred' my whole life. Easy to type with just the left hand. But if I need a second one, it's always 'ethyl'.
Whoosh!

chaoticlapras
Posts: 54
Joined: Thu Aug 21, 2014 8:51 am
Location: The Dimension known as England

Re: How do I learn C

Sat Jan 31, 2015 2:12 pm

ame wrote:Most programmers only ever write one program. They just modify it each time they need it to do something else.
Is there something I've been missing out on over the years?

In regards to OP, Geany is useful if you want a GUI, but really all you need is a command line text editor (I use nano, personally), and a compiler (going with gcc, which is pre-installed, I believe).

However, why just learn C? Personally, I started with C, but at a certain point it's better to move on to C++, which is backwards compatible, you can still work in a C style if you wish, but C++ is more suitable for writing larger programs, and is overall a better language.

Google 'learn C', most of the options are good ones. I can't remember where I started, but I remember distinctly getting to a point where I would just read a header file a few times then attack it with test programs.
This space is reserved for something actually useful, and will probably never be filled.

User avatar
PeterO
Posts: 4882
Joined: Sun Jul 22, 2012 4:14 pm

Re: How do I learn C

Sat Jan 31, 2015 3:25 pm

Don't take much notice of people who tell you language A is better than language B. (Especially when language A is Object Oriented. Not every problem needs the complexity of an OO language to solve it).

Learn enough languages to cover the types of programming you want to do, and use the one YOU find most appropriate for the task at hand.

PeterO
Last edited by PeterO on Sat Jan 31, 2015 8:37 pm, edited 1 time in total.
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4258
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: How do I learn C

Sat Jan 31, 2015 7:44 pm

I'd agree with that. And also don't get trapped into thinking you need to write pure object-oriented code in C++. As C with objects it works well, as an OO language it is by far the most confusing and complex language there is. It's only advantage is that it is compiled and therefore fast, otherwise other OO languages are far easier to learn.

You need to learn C first, anyway.

I would agree with the suggestion to get a copy of K&R -- well I would, except I've looked it up on Amazon and it is horrendously expensive and probably out of print. *sigh* It's still worth getting your hands on a copy if you can find it. Nothing else gives you the same detail.

Ravenous
Posts: 1956
Joined: Fri Feb 24, 2012 1:01 pm
Location: UK

Re: How do I learn C

Tue Feb 03, 2015 8:58 am

K & R may be out of print? That's a bit shocking. It's actually a good, clear read, the bits of it I've read anyway.

If so - and I wouldn't normally recommend this - but there is a PDF of one of the older editions available free on the web if you look for it. Though not the newest edition it's still good enough for most of the language, in my opinion.

User avatar
DougieLawson
Posts: 35594
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: How do I learn C

Tue Feb 03, 2015 9:00 am

When I read K&R it was a copy from my local lending library.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
rpdom
Posts: 14770
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: How do I learn C

Wed Feb 04, 2015 9:31 am

DougieLawson wrote:When I read K&R it was a copy from my local lending library.
You're lucky! My local library hasn't got a copy. In fact... (checking...) none of the libraries in the county have a copy. :(

Fortunately I do have my ancient copy on the table beside me :)

User avatar
DougieLawson
Posts: 35594
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: How do I learn C

Wed Feb 04, 2015 10:38 am

rpdom wrote:
DougieLawson wrote:When I read K&R it was a copy from my local lending library.
You're lucky! My local library hasn't got a copy. In fact... (checking...) none of the libraries in the county have a copy. :(
As the Librarian, there are ways to get loan copies from the British Library Lending Division. (They'll find a copy from some other county (like Hampshire).)
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
Redrobes
Posts: 80
Joined: Mon Dec 26, 2011 9:19 pm
Location: S.W. UK
Contact: Website

Re: How do I learn C

Wed Feb 04, 2015 7:06 pm

Never used these guys but if amazon is out then its available at this one.

http://www.bookdepository.com/C-Program ... 0131103627

Everyone who programs in C should have access to a copy.

User avatar
rpdom
Posts: 14770
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: How do I learn C

Thu Feb 05, 2015 12:24 am

DougieLawson wrote:As the Librarian, there are ways to get loan copies from the British Library Lending Division. (They'll find a copy from some other county (like Hampshire).)
Yes, I could do that, but there is a charge for interlending. It might be cheaper to get a second hand one from somewhere.

Return to “C/C++”