It's time to really learn C. Need some advice..
Oldbitcollector (Jeff)
Posts: 8,091
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...
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...
Comments
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!
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.
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?
Ray
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.
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.
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
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.
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.
He goes on to explain why and to describe a practical alternative. This guy doesn't mince words. I like that.
-Phil
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?
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.
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.
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.
"The Teach Yourself C in 21 Days" has been retitled. Apparently the publisher, Sams felt it takes a bit longer.
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. 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.
Ha, there is a companion book called "Teach Yourself C++ in 21 Years".
It still does not manage to cover everything about 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..
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
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 ?
Ray
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.
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.
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.
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:
Where as in C++ it can look like: 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! 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.
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.