Author Topic: Learning C  (Read 18015 times)

0 Members and 1 Guest are viewing this topic.

Offline Setar2k11Topic starter

  • Contributor
  • Posts: 16
  • Country: gb
Learning C
« on: April 20, 2012, 12:25:23 pm »
Hi could anyone recommend me some books on learning C. I have a reasonable understanding of assembler, but want to start to learn C. So far i have used some of the baseline PICs and the mid-range PICs. I have had a look on amazon but there so many to choose from, hoping someone could recommend some decent books. I would prefer a book to on-line tutorials as i prefer to have a book to refer to rather than reading off the internet.
 

Offline yanir

  • Regular Contributor
  • *
  • Posts: 216
  • Country: us
Learning C
« Reply #1 on: April 20, 2012, 12:47:58 pm »
C Programming Language (2nd Edition) [Paperback]
Brian W. Kernighan (Author), Dennis M. Ritchie (Author)


This is the gold standard book on the topic. Written by the actual developers of C. Unfortunately you will find some differences when programming on a pic, the libraries are a little different. But the data structures are all the same.

Being largely self taught with a few courses in c in college, i want to get this book as well to brush up on things I don't use very often like structs and type defs.

Another option:
Programming 16-Bit PIC Microcontrollers in C, Second Edition: Learning to Fly the PIC 24 [Paperback]
Lucio Di Jasio (Author)


I program pic parts in c (pic24 line). you will find some frustrating idiosyncrasies when trying to uses some standards taught in any book. I read through "programming 16 bit micro controllers in c" and it gives you a good general overview with some fun example projects"



yanir
« Last Edit: April 20, 2012, 12:53:08 pm by yanir »
 

Offline MikeK

  • Super Contributor
  • ***
  • Posts: 1314
  • Country: us
Re: Learning C
« Reply #2 on: April 20, 2012, 02:41:27 pm »
If you've never programmed in C, then do as yanir says, get the K&R book.  Although it's for computer programming, it will give you a solid understanding of the language, with nearly all of it carrying over to microcontroller programming.  It's a great book, there are even exercises in it to build your understanding.  There are probably a bunch of free C compilers (for the PC) that you can use to practice with.

Then, there are a bunch of PIC tutorials in C here: http://www.gooligum.com.au/tutorials.html
 

Offline Setar2k11Topic starter

  • Contributor
  • Posts: 16
  • Country: gb
Re: Learning C
« Reply #3 on: April 20, 2012, 04:52:53 pm »
Thank you, i have already come across the gooligum tutorials, an excellent website.
 

Offline A-sic Enginerd

  • Regular Contributor
  • *
  • Posts: 144
Re: Learning C
« Reply #4 on: April 20, 2012, 10:46:23 pm »
In addition to K&R, this one comes highly recommended.
http://www.amazon.com/C-Primer-Plus-5th-Edition/dp/0672326965/ref=sr_1_1?s=books&ie=UTF8&qid=1334961833&sr=1-1

I have the C++ version of this, and have talked with peers that have the C version and say they are on par with each other. Very good as an instructional book, but also put together such that it makes an equally valuable reference to have on the shelf.
The more you learn, the more you realize just how little you really know.

- college buddy and long time friend KernerD (aka: Dr. Pinhead)
 

Offline kiyotewolf

  • Contributor
  • Posts: 22
Re: Learning C
« Reply #5 on: May 27, 2012, 03:06:55 am »
C is hard to wrap your brain around, if you're used to other types of programming.

In assembly, unless you add in pre-built libraries, something like an overloaded operator, [i.e. something that can accept 8 bit unsigned integers, AS WELL as signed 8 bit integers, and even accepts 16 bit values - sketchy example..], has to be implemented, as different separate assembly routines, and you branch off, depending on what your inputs are, and what your outputs are.

They used to call C, a macro language for assembly.

You'd write C language code, and it would spit out a whole bunch of stuff, that was C in the IDE, but assembly, when it's all said and done.



/* Hello World program */

#include<stdio.h>

main()
{
    printf("Hello World");

}

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - cut here - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

/* Hello World program */
tags - their characters reverse, when going from an open comment tag, to a close comment tag

#include<stdio.h>
You tell the compiler, the # , that you want to INCLUDE, the console [text window interface program, no graphics or gui] support functions, so you can send messages out to the opened text window

main()
{
    printf("Hello World");

}

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - cut here - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

and another flavor, C# (C sharp.. *.cs type files), will be good to see the alternative

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - cut here - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

// A Hello World! program in C#.
using System;
namespace HelloWorld
{
    class Hello
    {
        static void Main()
        {
            Console.WriteLine("Hello World!");

            // Keep the console window open in debug mode.
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
    }
}


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - cut here - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

// A Hello World! program in C#.
Using C#, you just put two forward slashes, and till the end of the line, is a comment

using System;
same idea as the include statement, just written differently
namespace HelloWorld
Tells the compiler to treat the following code, as a particular, something..
I'd need help explaining this one better.

{
    class Hello
    again, need a helping hand explaining classes
    {
        static void Main()
        static means, the variables inside don't disappear, they exist, so they can be used again
       void, means, you can send the routine values, if you wanted, but it won't spit any back at you.
If you had

static int Main()

the program would be obligated, to RETURN a value.  The difference between a SUB and a FUNCTION, in QBasic, or a Method, and a <blank> in C variants.


        {
            Console.WriteLine("Hello World!");
            Console, dot, WriteLine, there is something called console, with an action called writeline, and the dot separates the thing, from the actions it is capable of doing.

            // Keep the console window open in debug mode.
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
    }
}

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - cut here - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

C language is CAPS LOCK sensitive!!!

If you write something, ..

PURPLE

and then

purple

THOSE ARE TWO DIFFERENT VARIABLES, or names of whatever.

Also,

computerMonitorTest

This is called, camel notation, the first word is lower cased, and the ones after, all have capital letters.

Learned this, from someone teaching me C# for XNA Xbox Live development.



~Paul

[I don't want stupid whiney comments that I did this poorly, or weakly, I TRIED DANG IT.  Deal with it.]

[also, I removed the CODE tags, cause it messed up my color tags]

P.S.

I have a PDF of C language, from old 70's computer magazines, goes into some detail about, the bare bones of C.

I need to look through my nOOk, and see what issue of the scanned magazine is, so I can properly link to the PDF that's online already.
« Last Edit: May 27, 2012, 03:17:06 am by kiyotewolf »
 

Offline Bored@Work

  • Super Contributor
  • ***
  • Posts: 3932
  • Country: 00
Re: Learning C
« Reply #6 on: May 27, 2012, 06:21:15 am »
C is hard to wrap your brain around, if you're used to other types of programming.

In assembly, unless you add in pre-built libraries, something like an overloaded operator, [i.e. something that can accept 8 bit unsigned integers, AS WELL as signed 8 bit integers, and even accepts 16 bit values - sketchy example..], has to be implemented, as different separate assembly routines, and you branch off, depending on what your inputs are, and what your outputs are.

Wrong. You are mixing C with C++. Which are not the same. I always find it funny when people can't even keep their languages separate, but then repeat the 40 year old rubbish statement

Quote
They used to call C, a macro language for assembly.

That statement was invented by some C haters who couldn't tell their opening brackets from their closing brackets and were still sucking mummies tits.

Quote
You'd write C language code, and it would spit out a whole bunch of stuff, that was C in the IDE, but assembly, when it's all said and done.

Every compiled language in those times, long before virtual machines became popular, was spitting out assembler code and would, applying that stupid criteria, have to be called a "macro language" for assembler, even FORTRAN.


To the OP, forget most of the rubbish information on the web about learning C. Forget about the one page "tutorials" you find on the web - the blind leading the blind. Forget about people who can't keep C and C++ separate. Forget the "For Dummies" books. Forget the "Teach yourself C in 12 Minutes" rubbish books. Forget about any of the stupid textbooks that strife more for not challenging the student, not hurting their delicate soles, making a big fuss about their modern teaching concept, than presenting the facts of the language correctly. You have to make a decision. If you really want to learn the language, then this requires some thinking, putting in some time, and, *shock* *horror* really reading (as in page by page, with brain engaged, and concentrating on the text). If you really want to learn the language, get a copy of "The C Programming Language" by K&R. Work through it.
I delete PMs unread. If you have something to say, say it in public.
For all else: Profile->[Modify Profile]Buddies/Ignore List->Edit Ignore List
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4245
  • Country: us
Re: Learning C
« Reply #7 on: May 27, 2012, 06:46:26 am »
Quote
C is hard to wrap your brain around, if you're used to other types of programming.
If you know some assembler, you won't go too wrong thinking of C as an intermediate between an assembler (specifically, a PDP11 assembler) and an algebraic language.
For example, a C statement  like
Code: [Select]
c = *p++;Is mysterious to many people who have used "typical" high level languages, but it corresponds quite closely to the "indexed addressing with post increment" address mode present in many computer architectures.  On a PIC18, it would be something like:
Code: [Select]
movff postinc1, c
 

Offline kiyotewolf

  • Contributor
  • Posts: 22
Re: Learning C
« Reply #8 on: May 27, 2012, 06:53:18 am »
@Bored@Work

Quote
[I don't want stupid whiney comments that I did this poorly, or weakly, I TRIED DANG IT.  Deal with it.]




"

If you really want to learn the language, then this requires some thinking, putting in some time, and, *shock* *horror* really reading (as in page by page, with brain engaged, and concentrating on the text).

"

Way to go insulting the OP.

And another thing, I sat down, with a simple tutorial, and modified XNA code, following the steps illustrated, and POOF.. I was programming in C. [C, C+, C++, C#, ALL VARIANTS OF ANSI C..]

I don't see you writing anything intelligent, or any pearls of wisdom, except a single book title.  I do see a bunch of snippy, immature remarks, and unneeded crude "humor," if that's what you call it, "Hero Member."
« Last Edit: May 27, 2012, 07:03:20 am by kiyotewolf »
 

Offline nctnico

  • Super Contributor
  • ***
  • Posts: 27418
  • Country: nl
    • NCT Developments
Re: Learning C
« Reply #9 on: May 27, 2012, 08:06:39 am »
Well, Bored@Work has a good point. Working through a good book and try the examples is the only way to learn a programming language properly.
« Last Edit: May 27, 2012, 08:52:15 am by nctnico »
There are small lies, big lies and then there is what is on the screen of your oscilloscope.
 

Offline kiyotewolf

  • Contributor
  • Posts: 22
Re: Learning C
« Reply #10 on: May 27, 2012, 08:33:50 am »
"

Working through a good book and try the examples is the only way to learn a programming language properly.

"

For some people..

You guys forget, you're here, cause of Dave's videos, and he's not a book, he's, a person, speaking, on video.

You can learn how to program, from more than one single book.. lectures, videos,..

Davecad..
 

Offline baljemmett

  • Supporter
  • ****
  • Posts: 665
  • Country: gb
Re: Learning C
« Reply #11 on: May 27, 2012, 12:08:07 pm »
You can learn how to program, from more than one single book.. lectures, videos,..

Learning to program and learning a language are two very different things!  As, indeed, are learning a language and learning a much more complex but more-or-less related language; trying to learn C by learning C++ or (worse) C# is doing things the hard way, as there's so much in the latter languages that simply doesn't even make sense in the C world.
« Last Edit: May 27, 2012, 12:12:42 pm by baljemmett »
 

Offline typeglob

  • Contributor
  • Posts: 44
  • Country: nl
Re: Learning C
« Reply #12 on: May 27, 2012, 02:41:18 pm »
Steer clear of object oriented languages like C++, C#, Java and even to a degree Javascript, Python, Ruby and Perl, PHP. You don't need the added complexity of OO (object oriented) programming when you're learning C.

If you learn C and learn it properly (understand the concepts, instead of just knowing when to use what statement) you will also be able to use other (similar) languages without too much trouble. Sure, the exact syntax might be different, but the concepts are not (OO adds quite a bit of extra complexity and new concepts though).

A lot of languages even look a lot like C (such as PHP, Java/C# and Javascript) while some others have a different syntax (like Basic and Pascal).

You can include assembler sections quite easily in C (should you want to) and you can also ask the C compiler to save the assembler output (based on your C program) just before it compiles it, so you can have a look at the assembler code the compiler generates. For GCC you use the -S parameter for that.

IMO, taking a 'shortcut' will turn out to cost you more time in the long run. With a shortcut I mean the type of books Bored@Work mentions at the end of his post. The K&R book mentioned by Yanir is the standard and it may be a bit dry at times, but there's a reason it is still used today. You really want the 2nd edition (which covers ANSI C) not the original version. And please be aware that modern compilers like GCC allow some C++ style statements in plain C.
« Last Edit: May 27, 2012, 02:44:51 pm by typeglob »
 

Offline Mechatrommer

  • Super Contributor
  • ***
  • Posts: 11698
  • Country: my
  • reassessing directives...
Re: Learning C
« Reply #13 on: May 27, 2012, 08:06:36 pm »
Quote
computerMonitorTest
This is called, camel notation, the first word is lower cased, and the ones after, all have capital letters.
Learned this, from someone teaching me C# for XNA Xbox Live development.
camel what? i used this technique alot and still is (way back 15yrs ago) lower case for local, upper case for global. who's your instructor's name? maybe i know him.
and have you seen Micro$oft notation back then? (dont know now) "glob_", "loc" "underscore" etc. you'll have a fun time programming by typing letters.
« Last Edit: May 28, 2012, 07:13:34 am by Mechatrommer »
Nature: Evolution and the Illusion of Randomness (Stephen L. Talbott): Its now indisputable that... organisms “expertise” contextualizes its genome, and its nonsense to say that these powers are under the control of the genome being contextualized - Barbara McClintock
 

Offline Simon

  • Global Moderator
  • *****
  • Posts: 17884
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: Learning C
« Reply #14 on: May 27, 2012, 09:26:58 pm »


"

If you really want to learn the language, then this requires some thinking, putting in some time, and, *shock* *horror* really reading (as in page by page, with brain engaged, and concentrating on the text).

"

Way to go insulting the OP.



While B@W can be quite blunt to the point of being deemed offensive, that point was not offensive to the original poster and a very good one. I personally find that these quick fix tutorials fix nothing and just end up putting people off because they promise you will learn something so fast but of course it cannot happen. This sort of scenario reminds me of my recent attempts to learn the Arduino where the instructions are presented in the form of a maze of tiny pages with hardly any information on and links to other stuff all over the place. That is not the way to teach a programming language to anyone.

I have actually become quite edgy about buying books on programming, because it seems very few actually know how to teach from the ground up. I bought two books on how to program pics in assembler, I resold both for a fraction of what I paid for them, in one of them the author spent more time making stupid jokes that he thought were funny in the first half of the book and in the second half he just reproduced most of the material on the microchip websites and fielded with programs that were not even explained. in the other book I'm not actually sure what the author ever said although it was about 800 pages long.

I have also decided to learn C, I was about to start a thread asking for recommendations on a decent IDE. Yes you're going to say GCC, well again just to keep things traditional it is not as simple as downloading it for windows. I have to actually compile it myself so that I can try out the examples in a book before I even know what compiling is really. I did find a program that creates a Linux type environment for GCC under Windows but that is not really what I was looking for. Any recommendations then on a proper IDE for a beginner? Or are there other iterations of the GCC program that is available to use out of the box?
 

Offline Simon

  • Global Moderator
  • *****
  • Posts: 17884
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: Learning C
« Reply #15 on: May 27, 2012, 09:34:02 pm »
how does PHP relate to C? I know that developing websites is now akin to programming, will learning see paved the way to learning PHP?
 

Offline Simon

  • Global Moderator
  • *****
  • Posts: 17884
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: Learning C
« Reply #16 on: May 27, 2012, 09:37:25 pm »
"

Working through a good book and try the examples is the only way to learn a programming language properly.

"

For some people..

You guys forget, you're here, cause of Dave's videos, and he's not a book, he's, a person, speaking, on video.

You can learn how to program, from more than one single book.. lectures, videos,..

Davecad..

if I am going to learn a language I want a book. Something I can carry around with me and pick up and refer to at any time. I think if you converted a proper book on C or any other language into videos it will work out a lot more viewing time and totally unmanageable for use as a reference. Having a video is great it can be more inspiring, but at the end of the day I want a book in my backpack I can pull out at any time I have time to do some reading.
 

Online Zero999

  • Super Contributor
  • ***
  • Posts: 19687
  • Country: gb
  • 0999
Re: Learning C
« Reply #17 on: May 27, 2012, 10:03:59 pm »
Here's a good tutorial.
http://computer.howstuffworks.com/c.htm

It's a bit dated in, recommending DJGPP which requires a DOS compatible operating system. Windows 64-bit users will either need to use an emulator (DOSBox, VirtualBox etc.) or use a free native Windows compiler such as MinGW.
 

Offline TerminalJack505

  • Super Contributor
  • ***
  • Posts: 1310
  • Country: 00
Re: Learning C
« Reply #18 on: May 27, 2012, 10:30:57 pm »
Are you looking for an IDE to learn C in Windows?  If so then I recommend QT Creator.  It uses the GCC toolchain but you'll never need to worry about that.  And, yes, you can create plain-old C projects with it.  The IDE is cross-platform and works with Linux and OSX as well.

You don't have to compile anything to get the IDE working.  Just install and go.

Of course there is Visual Studio Express as well.
 

Online IanB

  • Super Contributor
  • ***
  • Posts: 12038
  • Country: us
Re: Learning C
« Reply #19 on: May 27, 2012, 11:12:36 pm »
If you want to do C in Windows, then Visual C++ 2010 Express is an excellent choice, free and very solid. (Ignore the C++ bit, it can do C as well.)

I can do a few screen shots for you showing how to write and run the hello world program. It will get get you over the "where do I begin" dilemma that happens with all modern tools. But once you know, it's easy.
« Last Edit: May 27, 2012, 11:14:27 pm by IanB »
 

Online IanB

  • Super Contributor
  • ***
  • Posts: 12038
  • Country: us
Re: Learning C
« Reply #20 on: May 28, 2012, 12:01:01 am »
Here's a basic walk through.

The steps should correspond to the order of attachments:

1. Launch Visual C++ 2010 Express. Click New Project...
2. Choose "General" and "Empty Project". Give it a name ("hello" in this case).
3. Under Source Files, Add a New Item...
4. Give your new file a name ("hello.c" in this case). The ".c" extension is important as it makes this a C file not a C++ file.
5. Enter your code.
6. Before running the program, make a change to the project settings
7. Under Linker > System change the SubSystem type to Console. This will cause the program output to remain visible when the program finishes.
8. Now you can build your program to see if it compiles OK
9. ...and if it does, you can run it...
10. ...and see the results

 

Offline pkrobot

  • Contributor
  • Posts: 12
  • Country: us
Re: Learning C
« Reply #21 on: May 28, 2012, 02:39:52 am »
K&R is a wonderful book. I have bought books on programming, and I've gotten rid of them when they became obsolete, but I still have my copy from the school days, and still consult it from time to time.

Reading it from cover to cover is not easy. Even if you read it, you won't retain everything. The best thing is to do a project, and get going. Hopefully, you'll stick to C (or C++), and eventually with practice and experience, become a very good programmer.

I like Visual Studio, and occasionally use it for unit testing, but if you want to really learn C, then a better option is to install cygwin on your PC, and use Eclipse IDE. Better still, get started on Arduino. I've always found that just looking at lines being printed on the screen, blinking LED's and making motors spin is always more interesting.

-pk
 

Offline calin

  • Regular Contributor
  • *
  • Posts: 240
  • Country: us
Re: Learning C
« Reply #22 on: May 28, 2012, 06:46:38 am »
There are few books that are "the bible" of  someone who knows C/C++ not only to learn it

- C : Progamming in C / K&R
- C++ - The C++ Programming Language / Stroustrup
- C on UNIX systems (if you want to use embedded Linux this is II) - Advanced Programming in the UNIX environment / Stevens  and UNIX Network Programming

And stay away from an IDE .. it makes you "dumb" :) . Joke aside do make the effort to write your own Makefile. It will pay off in the long run trust me.  For your own sanity .. learn C on a UNIX system first; not the UNIX API's ... the language itself.  If you want to truly learn to respect C put this options in the compiler "-ansi -pedantic"  you will hate them but they are "good".




 

Offline Simon

  • Global Moderator
  • *****
  • Posts: 17884
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: Learning C
« Reply #23 on: May 29, 2012, 09:47:35 am »
Are you looking for an IDE to learn C in Windows?  If so then I recommend QT Creator.  It uses the GCC toolchain but you'll never need to worry about that.  And, yes, you can create plain-old C projects with it.  The IDE is cross-platform and works with Linux and OSX as well.

You don't have to compile anything to get the IDE working.  Just install and go.

Of course there is Visual Studio Express as well.

Well I'm trying to install QT but well with a 1.7 GB install file that is interesting. A straight download never completes and they have rigged it so that a download manager does not work. Doing the webinsall is taking hours and it looks like i will be putting a nice load on THEIR servers downloading it 3 times for each of my machines (assuming I use it). hopefully the program itself is better than their method of distribution.
 

Online IanB

  • Super Contributor
  • ***
  • Posts: 12038
  • Country: us
Re: Learning C
« Reply #24 on: May 29, 2012, 05:36:02 pm »
Well I'm trying to install QT but well with a 1.7 GB install file that is interesting. A straight download never completes and they have rigged it so that a download manager does not work. Doing the webinsall is taking hours and it looks like i will be putting a nice load on THEIR servers downloading it 3 times for each of my machines (assuming I use it). hopefully the program itself is better than their method of distribution.

I read the recommendations above about Cygwin and Eclipse, or QT Creator, and I have to wonder, "why, man, why?"

Learning programming does not have to be torture to be useful. You want to be learning about C, not about various uncommon and peculiar programming environments.

My recommendation to use Visual Studio was not given in jest. Visual Studio is heaps better and more polished than anything else you will find, and what's more it just works. No muss, no fuss. Install it, use it, be happy.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf