Shop OBEX P1 Docs P2 Docs Learn Events
It's time to really learn C. Need some advice.. — Parallax Forums

It's time to really learn C. Need some advice..

Oldbitcollector (Jeff)Oldbitcollector (Jeff) Posts: 8,091
edited 2014-09-09 11:18 in General Discussion
My latest Propeller creation has me programming in two completely different worlds, Propeller & PIC32.

I've come to the conclusion this morning that it's time to really start getting a better handle on C.
(Currently, I know just enough C to be dangerous, really dangerous.)

At my disposal is:

* my desktop computer running Linux (plenty of C compiler support there)
* a Propeller which is now compatible with C, thanks to SimpleIDE
* a few PIC32 chips with a copy of MPLAB-XC

I suspect the correct way forward is to crack open The C Programming Language, by Ritchie and start working at at my desktop computer.
or.. I could with http://learn.parallax.com/propellerc (Is this material far enough along to be a good starting point?)

The end result will be for programming supporting applications and alternative micros. I'm quite content to program the Propeller in Spin until the end of time.

How relevant is material from "The C Programming Language" book to something like propellerC or even PIC programming?
Can you draw straight lines between them, or are there language differences?

Thank you fellow C'ans for you answers...
«13

Comments

  • Heater.Heater. Posts: 21,230
    edited 2014-08-01 10:02
    "The C Programming Language" describes C the language. As such pretty much all of it is relevant. If it's the best style for a particular individual to learn C is another matter. It's all I had when I started with C and I loved it.

    Having said that "K&R", as we affectionately call it, makes use of things like printf for output which is fine on a PC but those library functions are very big for the tiny memory space of the Propeller. Then there are the file handling functions, I'm not actually sure how much of that is supported by propgcc.

    So, in summary the K&R book is excellent for learning C but some of the facilities it's example code use may not be appropriate on embedded systems. propgcc for example provides very much cut down I/O function so as to conserve memory space.

    Then of course on embedded systems we may have to worry about memory layout and such. Where is the RAM? Where is the FLASH? Usually taken care of by the compilers for the device but you won't see it mentioned in K&R. In the Propeller we have HUB RAM and COG space. We can execute code from within COG or from HUB (Using LMM) so propgcc provide facilities for arranging that. Again not something you would expect to find in K&R or any C tutorial book.

    So I guess, you have to do both. Study the K&R and practice things on a PC. Lear the Parallax tutorials and practice things on the Propeller!


  • Heater.Heater. Posts: 21,230
    edited 2014-08-01 10:35
    Having said all that I said. I was thinking....

    It would be a very good thing if some enthusiastic and Propeller loving person who is not familiar with C, such as yourself, would undertake the study of C on the Propeller armed only with the materials provided my Parallax.

    What I'm thinking is that writing such educational material is quite an undertaking and I'm sure it could do with some "proof reading", a run through. I'm not suggesting that there is anything wrong with it but at some point it needs to be know if it actually works or not. If I were to write such things they would never work for a beginner, I'd be assuming so many "obvious" things that are perhaps not at all obvious to anyone who has not been there before.

    In the Arduino land for example they use C++. They have very good a simple tutorials and documentation for beginners. They carefully avoid all the complex parts of C++. They don't expect people to go and read Bjarne Stroustrup's books (The equivalent of K&R for C++)

    So. I change my mind. If you are into checking out Learn materials and helping out the Parallax community forget about K&R.
  • jazzedjazzed Posts: 11,803
    edited 2014-08-01 11:01
    Can you divide things up into a curriculum?

    My impression is that Parallax wanted a certain configuration for SimpleIDE for education so that teachers can focus on the entry level tutorials for learning the language, and getting results/satisfaction with the ActivityBot. In some colleges, class names like this would be considered ABC1A. Where ABC1B, ABC2A, ABC2B, etc... are more advanced and have not been written yet. Clearly the Parallax tutorials are aimed at ABC1A. If CS1A was Introduction to C programming, how would ABC1A be different from CS1A? Well, it's introduction to C programming with the ActivityBot where physical feedback using a microcontroller enhances the learning experience.

    So, a syllabus for ABC1A is basically the Propeller-C learn tutorials. A syllabus for CS1A would be different, and focus more on a broad view of the language syntax and computer solving problem applications. I think it's instructive to examine both, but I can't spend all day on it and meet my deadlines too .... Maybe someone else can write a CS1A syllabus in the meantime?
  • RsadeikaRsadeika Posts: 3,837
    edited 2014-08-01 11:09
    I would also recommend that you use the Learn site tutorials and use the K&R book as reference, to get a handle on some of the major concepts in C. I believe that their have been a couple of people here that have used the Learn site to successfully learn the SimpleIDE way of programming the Propeller.

    Ray
  • Heater.Heater. Posts: 21,230
    edited 2014-08-01 11:28
    But that's my point Ray. From the point of view of someone who has perhaps never seen a micro-controller before or is totally new to programming, never mind C, the K&R thing may be just a bit to much. Enter people like the Arduino creators who take it from the beginning in very simple steps. I have always imagined that was the point behind SimpleIDE and the Parallax materials.

    Turned out that SimpleIDE became ComplexIDE (As compared the Arduino IDE) but my point still stands.
  • jazzedjazzed Posts: 11,803
    edited 2014-08-01 11:39
    Heater. wrote: »
    Turned out that SimpleIDE became ComplexIDE (As compared the Arduino IDE) but my point still stands.

    I'm adding a feature that should fix that problem you had. It serves everyone better to do it.
  • Heater.Heater. Posts: 21,230
    edited 2014-08-01 12:05
    Jazzed,

    That's not quite what I meant.

    SimpleIDE became ComplexIDE with the support of more than one language. With a bewildering choice of board types. With multiple "modes" for C compilation. And whatever else it is that prevents one from just starting the IDE, typing the code and hitting run.

    I can of course appreciate why all those things are in there and probably need to be. Starting from the fact that there is no "one true Propeller board" as is the case for the Arduino.
  • jazzedjazzed Posts: 11,803
    edited 2014-08-01 12:51
    Heater. wrote: »
    Jazzed,

    That's not quite what I meant.

    SimpleIDE became ComplexIDE with the support of more than one language. With a bewildering choice of board types. With multiple "modes" for C compilation. And whatever else it is that prevents one from just starting the IDE, typing the code and hitting run.

    I can of course appreciate why all those things are in there and probably need to be. Starting from the fact that there is no "one true Propeller board" as is the case for the Arduino.


    Hi Heater,

    "just starting the IDE, typing the code and hitting run" ... this is the goal for single C source file programs.

    Parallax has tried very hard to make SimpleIDE simpler for users than it started (vastly complicating the once simple implementation). Aspects of it also needed to be changed to meet the education program requirements.

    It is not necessary to use the project manager for example, but you can ... It is however still necessary to have a project associated with a C file, and the last scheduled enhancement in this area for release 1.0 is to deal with this issue by automatically creating a project for C files having a main() when a project does not already exist.

    In the interview with Ken Gracey, I made it clear that "full on Spin programming" is being transitioned out of SimpleIDE to PropellerIDE (which is becoming a Parallax product), and Spin will only be an enhancement for Propeller-C SimpleIDE programs. In a future release we will be able to call Spin binary objects directly without any user translations. This does not mean Parallax support for Spin is going away by any means; focus on Spin is being redoubled.

    Of course Arduino also supports multiple board types. They put that in a menu item, we put it somewhere else.
  • twm47099twm47099 Posts: 867
    edited 2014-08-01 14:12
    Heater. wrote: »
    Having said all that I said. I was thinking....

    It would be a very good thing if some enthusiastic and Propeller loving person who is not familiar with C, such as yourself, would undertake the study of C on the Propeller armed only with the materials provided my Parallax.

    What I'm thinking is that writing such educational material is quite an undertaking and I'm sure it could do with some "proof reading", a run through. I'm not suggesting that there is anything wrong with it but at some point it needs to be know if it actually works or not. If I were to write such things they would never work for a beginner, I'd be assuming so many "obvious" things that are perhaps not at all obvious to anyone who has not been there before.

    In the Arduino land for example they use C++. They have very good a simple tutorials and documentation for beginners. They carefully avoid all the complex parts of C++. They don't expect people to go and read Bjarne Stroustrup's books (The equivalent of K&R for C++)

    So. I change my mind. If you are into checking out Learn materials and helping out the Parallax community forget about K&R.

    I think I fall into that category. While I tried to learn C on my own back in the 1980's and did read K&R, I never got to the point where I knew when to use a '&' or a '*'. I haven't used C since then, until Santa brought me an ActivityBot.

    I have gone through many of the tutorials, and they have helped me learn propC. This forum has been an important resource since the tutorials leave me wanting more advanced topics. I've been able to program a few extensions to the tutorials (bluetooth interface, Pixy, expanded SIRC among others). So in that way I think it has been successful. I have had to look up the syntax of some functions on the net, but there are many useful sources.

    I would like some more detailed tutorials on different interfaces such as I2C, etc, and more detailed explanation of what is going on in the different Libraries (abdrive for example). But those would be topics for more advanced courses.

    Probably the thing I find less satisfactory is the organization of the tutorials. They're not very good as a reference for going back to try to find something. Hopefully, they will eventually have some sort of index, but I realize they have higher priorities.

    Tom
  • GordonMcCombGordonMcComb Posts: 3,366
    edited 2014-08-01 15:19
    I'll take the dirt road here and say you do not need the K&R book, or any other book, to learn C for a microcontroller. You won't use much of what you'll learn, because 97%+ of what you'll do is with a small subset of the language. If anything, you can read just the first chapter of a C book, and come away with the basic syntax knowledge you need.

    The rest you'll glean by examples and other material on the Web WRITTEN FOR MICROCONTROLLERS.

    (This is to say that if you already know C, you can adapt that knowledge to working with microcontrollers. But the inverse approach of learning C just to write for microcontrollers wastes valuable time. It's like going to med school just so you can give aspirin to your kids.)

    You may be aware that Parallax provides shortcut libraries for Propeller C, the same kind of idea behind the Arduino. These are not in any C language book. Don't worry; you can open the library and see how it works, should you want to learn more. Not surprisingly, the common library routines provided by Parallax look oddly like the programming statements for the BASIC Stamp.

    You can also, if you wish, bypass these libraries and write "straight C." No, it won't make you a better Propeller programmer to start out this way, no matter what the purists say. But it could be an interesting project later on when you have a good grasp of all the fundamentals.

    I'll also suggest going to Parallax's very own learn site, learn.parallax.com, as you point out in your post. It's probably for more of a raw beginner than you are, but it gets the job done. You also personally know the guy who wrote most of it, and if there are parts that you think are missing or could be elaborated on, I'm sure Andy would love to entertain suggestions. You'll never get that from the other C book authors.
  • Heater.Heater. Posts: 21,230
    edited 2014-08-01 15:31
    Gordon,

    97% seems a bit excessive. Well a lot excessive. I might agree if you were talking about C++, that is a huge and infinitely complex language. Before we even start to think about the STL.

    I'm pretty sure I have used most of the C language in micro-controllers over the years. Can you say what some of that 97% people are not going use is?

    I do agree though, one should be able to get by, or at least make a very good start by reading micro-controller oriented tutorials. Preferably the propeller ones. There is a risk of getting bogged down in the details of some other MCU architecture and hardware support that do not apply to the Propeller.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2014-08-01 15:41
    A rather dated introductory C book that suits my temperament is Practical C Programming, by Steve Oualline (an O'Reilly publication). From the book:
    "The function scanf works like printf, except that scanf reads numbers instead of writing them. scanf provides a simple and easy way of reading numbers that almost never works. ..."

    He goes on to explain why and to describe a practical alternative. This guy doesn't mince words. I like that.

    -Phil
  • GordonMcCombGordonMcComb Posts: 3,366
    edited 2014-08-01 15:57
    Heater. wrote: »
    Can you say what some of that 97% people are not going use is?

    Learn the basics -- core syntax, conditionals, loops, data types, casts, and so on. You can master those in a day, and you can then pick up the extras as you go. What's the point of the Internet if you can't look things up when you need them?

    I also said, "
    97%+ of what you'll do is with a small subset of the language." Do you really think that's the same thing as what you replied with?
  • jazzedjazzed Posts: 11,803
    edited 2014-08-01 18:56
    So what is your approach OBC?

    Yes, use http://learn.parallax.com/propellerc ... it's a good start. Take notes on things you don't understand.

    Then when you see something in a program that you don't get, just ask about it here.
  • Oldbitcollector (Jeff)Oldbitcollector (Jeff) Posts: 8,091
    edited 2014-08-01 19:18
    I think I've created battle plan..

    I'm going to take Gordon's advice and work the first part of the K&R book for the basics, then after I read over some source code that is of interest to me to see what I can understand of it., I'll launch into PropellerC.

    Expect questions.
  • NWCCTVNWCCTV Posts: 3,629
    edited 2014-08-01 20:53
    I have always found the Teach Yourself (Insert Language) In 21 Days a great resource when learning a new language. It is not as advanced as other material but it gets right to the point.
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2014-08-01 21:42
    A rather dated introductory C book that suits my temperament is Practical C Programming, by Steve Oualline (an O'Reilly publication). From the book:
    "The function scanf works like printf, except that scanf reads numbers instead of writing them. scanf provides a simple and easy way of reading numbers that almost never works. ..."

    He goes on to explain why and to describe a practical alternative. This guy doesn't mince words. I like that.

    -Phil

    Well, regardless of whether you use Kernighan and Richie or Oualline, the scanf is just about the worst input scheme you can use with a microcontroller as it takes a huge chunk of resources. Printf is not much better in the output side. (Try them out, but consider future projects without them.)

    Regardless of what you study, the scanf, printf, and floating point are all memory intensive. If you stay with integer maths and avoid conversions that occur at the input and output, your programs will be smaller and run faster.

    Like Stevel Oualline say above..... "scanf provides a simple and easy way of reading numbers that almost never works. ..."

    Techinically, the Libraries are NOT the C language. And scanf and printf are library functions that might just best be avoided, or at least handled with caution.
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2014-08-01 21:46
    NWCCTV wrote: »
    I have always found the Teach Yourself (Insert Language) In 21 Days a great resource when learning a new language. It is not as advanced as other material but it gets right to the point.

    "The Teach Yourself C in 21 Days" has been retitled. Apparently the publisher, Sams felt it takes a bit longer.
  • Heater.Heater. Posts: 21,230
    edited 2014-08-01 21:55
    Gordon,

    Yes, sorry, I misread your post. My reading skills really go into the bucket after not sleeping for a couple of days. Turns out I'm in complete agreement with you.
    What's the point of the Internet if you can't look things up when you need them?
    Indeed. Type any likely sounding query into google re: C or any other language/programming issue now a days and there is almost certainly a similar question already on stack overflow with a bunch of solutions offered.

    Be warned though. Many times I have noticed that the most voted up answers on stack overflow are actually not optimal, contain subtle problems that may reveal themselves in odd cases or are down right wrong. Meanwhile, better answers that perhaps came in a lot later never get voted up much. It pays to look over all the answers and double check the likely ones.

    Same applies to most of the info on internet I guess. But stack overflows response rating system may lull one into believing the "crowd" has verified the answers.
  • Heater.Heater. Posts: 21,230
    edited 2014-08-01 21:59
    Loopy,

    Ha, there is a companion book called "Teach Yourself C++ in 21 Years".

    It still does not manage to cover everything about C++.
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2014-08-01 22:09
    After quite a bit of research and thought, I think I have resolve the problems of learning C++.

    I simply decided it isn't really a computer language. It a method of obfuscating C programs so that they are no longer available to the general public.

    Arduino uses C++ so that the user will wait for someone else to write a library for everything they buy and want to use. Whereas, real C programmers merely write their own as need arises..
  • whiteoxewhiteoxe Posts: 794
    edited 2014-08-02 15:05
    I found a relatively inexpensive book on C that I found much more readable than the C in 21 days my brother loaned me. C Programming Absolute Beginner's Guide , Its about $15 for Hard copy or kindle. Roughly 330 pages. Its all I needed plus the little bonus for me was the authors suggestion to use code:blocks IDE , windows or Linux and in Linux it was I the Mint repository.
    Nice book to read. I'm not sure id like to read that K&R book. Too much information. didn't some one say 97% didn't apply to Prop ;)
  • whiteoxewhiteoxe Posts: 794
    edited 2014-08-02 15:17

    Arduino uses C++ so that the user will wait for someone else to write a library for everything they buy and want to use. Whereas, real C programmers merely write their own as need arises..

    Loopy, why would it make any difference for a library to be written if the Arduino used C instead of C++ . And as the Arduino is aimed at beginners such as 'dancers' rather than programmers ;) , their own words in some promo material..., why would they have not just stuck with C rather than C++? I'm asking this because what I have heard from somewhere...,was that C++ is C with extra functionality or something like that. I can see how that might be good for a programming language on a PC but I don't see how it would add anything to programming on a little atmega chip ?
  • RsadeikaRsadeika Posts: 3,837
    edited 2014-08-02 15:40
    C Programming Absolute Beginner's Guide...
    So, how much of this book applies to the Propeller? If the K&R book is to much information, what the heck are you reading in that 330 pages? After seeing some of the suggestions here, the OP might have to decide if you want learn how to program the Propeller using C, or do you want to learn how to just program in C? If you try the mix the two too much, you just might end up in the weeds wondering what the heck is going on.

    Ray
  • whiteoxewhiteoxe Posts: 794
    edited 2014-08-02 16:11
    Ray, I just added in my2 cent opinion. The OP is far more capable than myself at making these decisions. I had not heard of K&R before. It may be a must have reference for C programmers. I imagine it proably has more in it than C in 21 days , which I found difficult to follow . Im just suggesting that latest book as I didn't find it hard to follow. I think its going to be a lot simpler than some, like you said you don't want to mix the two too much. So maybe the easy read on C isn't as bad option ?
  • jazzedjazzed Posts: 11,803
    edited 2014-08-02 16:21
    whiteoxe wrote: »
    Ray, I just added in my2 cent opinion. The OP is far more capable than myself at making these decisions. I had not heard of K&R before. It may be a must have reference for C programmers. I imagine it proably has more in it than C in 21 days , which I found difficult to follow . Im just suggesting that latest book as I didn't find it hard to follow. I think its going to be a lot simpler than some, like you said you don't want to mix the two too much. So maybe the easy read on C isn't as bad option ?


    The K&R texts (ANSI-C and the original) are very small and easy to read. They are straight to the point, so may seem a bit terse to some. The ANSI-C version is recommended. If you know C, Propeller-C is cake. If you know Propeller-C you are on your way to learning C. Usually a large text is full of library documentation or a bunch of homework questions. The K&R texts do not feature homework questions.
  • Heater.Heater. Posts: 21,230
    edited 2014-08-02 21:28
    I'm amazed to find K&R being described as essentially "bloated".

    K&R is a very concise description of what is not a very big language. It's one of the slimmest books about using a programming language you will ever find on the bookshelves.
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2014-08-02 21:53
    whiteoxe wrote: »
    Loopy, why would it make any difference for a library to be written if the Arduino used C instead of C++ . And as the Arduino is aimed at beginners such as 'dancers' rather than programmers ;) , their own words in some promo material..., why would they have not just stuck with C rather than C++? I'm asking this because what I have heard from somewhere...,was that C++ is C with extra functionality or something like that. I can see how that might be good for a programming language on a PC but I don't see how it would add anything to programming on a little atmega chip ?

    You say Arduino is made for 'dancers'.
    Why would they have not stuck with plain C?
    I don't really know. But it seems that they wanted to make non-technical people comfortable with the Arduino by hiding the fact that it was using C++.

    It all started with another IDE called Wiring. The concept seems to be to insulate the user from anything that seems too technical.

    I suppose one could add on a library in plain C rather than C++. But regardless, there is a lot that is just 'the Arduino way' of doing things. For students wanting to learn mainstream C, they will have to backtrack some.

    In some ways, I feel more comfortable with learning to use generic C and the AVR IDE rather than bother with the Arduino overlay. You can still use all their boards and stuff, but you are learning GCC as industry uses it. You acquire skills that fit into a resume.

    And the Propeller offers generic C or C++ in GCC. It is up to you what you want to learn and why.
  • Heater.Heater. Posts: 21,230
    edited 2014-08-02 22:17
    Loopy,
    You say Arduino is made for 'dancers'. Why would they have not stuck with plain C?
    Because C++ makes it possible to create simpler interfaces (APIs) to things for non-technical users.

    Let's take the canonical example of the serial port.

    If I have more than one serial port to deal with and I'm writing my code in C any library provided to help me will look like:
    char = serialGetChar(serialPortPtr_1);
    serialPutChar(serialPortPtr_2, char);
    

    Where as in C++ it can look like:
    char = serialPort1.getChar();
    serialPort2.putChar(char);
    
    Notice how the C version requires some handle or pointer to be used on every API call? C++ hides all that Smile.

    Notice how the C++ version looks remarkably similar to what we do in Spin on the Propeller?
    Apart from the semicolons and assignment operator that could be Spin!
    ..it seems that they wanted to make non-technical people comfortable with the Arduino by hiding the fact that it was using C++
    I think you are right. I could never actuall find any mention of "C++" on the Arduino site when I looked in the past. I was totally confused as to what language they were using what with all this guff about "processing" this and "wiring" that and "sketches".

    Still I think they were wise and did a good job in using C++ and not documenting any of the hard parts.
  • jazzedjazzed Posts: 11,803
    edited 2014-08-02 23:34
    This is one of my favorite online C books: http://publications.gbdirect.co.uk/c_book/

    A good C tutorial is here: http://www.cprogramming.com/tutorial/c-tutorial.html
    A good C++ tutorial is here: http://www.cplusplus.com/doc/tutorial/
    There isn't much difference really except for the class concepts.

    It is not a waste of time to look at these if you want to learn C programming.
    If you think it's a waste of time, well move along to something you like.
Sign In or Register to comment.