Author Topic: Want to learn "C"  (Read 14462 times)

0 Members and 1 Guest are viewing this topic.

Offline IanB

  • Super Contributor
  • ***
  • Posts: 9692
  • Country: us
Re: Want to learn "C"
« Reply #50 on: October 20, 2012, 07:23:52 pm »
Sigh. Please try to speak about the right thing. printf() has got nothing to do with C language as such.

However, printf() is part of the standard library and along with the rest of the standard library is described precisely by the language standard. A standard-conforming C implementation is expected to provide the standard library exactly as specified. If you have a C compiler without all the components defined by the language standard you don't have a proper C environment and you can't follow examples from a C tutorial.

As has been stated above, this is something that experts may understand and take in their stride, but it is something that may be confusing for a beginner. If you cannot pick up a copy of K&R and work through all the examples and exercises as given, you don't have a working C environment.
I'm not an EE--what am I doing here?
 

Offline Kremmen

  • Super Contributor
  • ***
  • Posts: 1283
  • Country: fi
Re: Want to learn "C"
« Reply #51 on: October 20, 2012, 08:01:15 pm »
Well. printf() comes as a standard in some environments and not in others. It is a library function and has zip to do with the core language definition. It is included in environments that naturally support files and streams. Also,and perhaps mor to the point, it has zip to with the compiler, exactly because it is not a builtin language construct, but a _library_ _function_. Someone long ago decided that it would be nice to print things on console and into files and included that in the stdlib and yes, in the part of standards describing stdlib. But if you don't have a console and don't have a file system, printf() is about as useful as the pope's balls. That's why you seldom find it in an embedded development environment.

K&R is a good if old source (it does not describe the current C standard) and its examples reflect that fact. But if you sorely must have printf() in Arduino to run the examples, there is no technical reason why it cannot be done. It will be needlessly wasteful of the meager resources but it definitely can be done.

I disagree with your last statement. Presence of printf() in stdlib does not define a working C environment. In fact i am not sure if all the K&R examples would even compile as written, using a recent (say C99 or newer) standard compiler. But i haven't checked and don't really care.
Nothing sings like a kilovolt.
Dr W. Bishop
 

Offline JuiceKing

  • Regular Contributor
  • *
  • Posts: 233
  • Country: us
Want to learn "C"
« Reply #52 on: October 20, 2012, 09:35:30 pm »
Sigh. Please try to speak about the right thing. printf() has got nothing to do with C language as such.

However, printf() is part of the standard library and along with the rest of the standard library is described precisely by the language standard. A standard-conforming C implementation is expected to provide the standard library exactly as specified. If you have a C compiler without all the components defined by the language standard you don't have a proper C environment and you can't follow examples from a C tutorial.

As has been stated above, this is something that experts may understand and take in their stride, but it is something that may be confusing for a beginner. If you cannot pick up a copy of K&R and work through all the examples and exercises as given, you don't have a working C environment.

Exactly.
 

Offline andersm

  • Super Contributor
  • ***
  • Posts: 1148
  • Country: fi
Re: Want to learn "C"
« Reply #53 on: October 20, 2012, 10:00:52 pm »
A standard-conforming C implementation is expected to provide the standard library exactly as specified.
That's true of hosted implementations. Freestanding implementations only have to provide <float.h>, <limits.h>, <stdarg.h>, <stddef.h>, <iso646.h>, <stdbool.h> and <stdint.h>.

Offline westfw

  • Super Contributor
  • ***
  • Posts: 3195
  • Country: us
Re: Want to learn "C"
« Reply #54 on: October 21, 2012, 02:32:02 am »
And in fact arduino has printf() (or will allow you to use printf()), but you'll have to set it up in mysterious ways before you can use it (sort of like using JCL to define the IO of your ForTran programs on the IBM mainframe.)

Look, there are about three pieces...

1) There is the syntax of the C language itself.  Syntax, Functions, Variables, control statements, operators, and so on.  This is probably best learned on a desktop, with a debugger handy, using a reference like K&R.  There are a bunch of principles here common to many computer languages.  If you've programmed in any other language before, or had one of the "Computer Science 101" classes, this should not be all that strange.  If you don't have ANY programming background, you might want to look around for a basic programming or computer science class (exact language not too important.)  Be aware that many classes, and books, jump right into manipulation of images on your desktop screen, because that's the fun part.

2) There are a bunch of concepts that are key to programming microcontrollers that aren't so common in desktop environments.  IO Registers.  Bit Masks vs bit numbers, bit shifting, and bit-banged IO.   Volatile variables.  Memory limitations.  Interrupts.  Concurrency (and the lack thereof.)  The low speed of some peripherals.  Locations that you write a 1 to to set them to zero.  What peripherals do and what are their limitations.  The fact that exit() isn't going to stop your program, and terrible bugs aren't going to put a nice error message on your screen.
This is probably the hardest part.  If you have a chip/electronics/assembler background, it's not too difficult to add to the basic C knowledge, but books and tutorials that deal with these things are a lot rarer than books on Excel...

3) A particular environment will have a set of library functions that it uses for doing environment-specific things.  This is true whether you're talking about Standard C functions like printf(), Posix functions (an operating system standard), arduino, cmsis, Atmel Software Framework, Stellarisware, or whatever.  Arduino gets some negative press for "hiding important things that you should learn", but I don't see that it's that much different than vendor-provided libraries.  (A vendor training video I watched recently had a sample program that used both LED_Toggle(LED0) and gpio_toggle_pin(NHD_blahblah_BACKLIGHT).  Both to blink LEDs...)  Arduino uses the same standard avr-libc that most people programming AVRs in C end up using, and adds some additional functions.
A lot of "modern" programming seems to be about learning what library functions exist, and how to get them and use them.  Cause those GTK windowing classes are so much more advanced than the KDE `toolkits, you know.  (not that this is really "modern"; back in the Fortran/COBOL/PL1 days, a lot of learning a language was about using the IO facilities of the language.  But in those days those were part of the language, while now you can pick and choose different libraries that behave somewhat differently.)

And that will get you started.  You still won't be able to immediately connect up that weird 9 DoF sensor chip and make an autonomous quadcopter assassin-bot, because you'd need to understand how that chip works (unless someone else has already done it and is willing to share their code, which is somewhat more likely in the arduino world than anywhere else these days.)  And even after you've done that, autonomous robot assassins are hard research projects that DoD pays large teams big bucks to try to figure out...

Quote
A standard-conforming C implementation is expected to provide the standard library exactly as specified.
And one of the first things you need to learn if you want to program C on microcontrollers is that that isn't how it's going to be.

Quote
In Arduino you can use the write() and writeln() functions instead.
print and println class methods, actually (so "Serial.print(...)" rather than just "print(...)")
(writeln is Pascal...)
« Last Edit: October 22, 2012, 07:34:31 am by westfw »
 

Offline Kremmen

  • Super Contributor
  • ***
  • Posts: 1283
  • Country: fi
Re: Want to learn "C"
« Reply #55 on: October 21, 2012, 07:10:58 am »
[...]
print and println class methods, actually (so "Serial.print(...)" rather than just "print(...)")
(writeln is Pascal...)
And so it is of course :) I'm confusing Arduino and Delphi Pascal here, i see. Been doing Delphi in the past and still consider that a gentleman's environment, however out of the picture it may be in these Java days.
Otherwise your post reflects my thoughts precisely.
Nothing sings like a kilovolt.
Dr W. Bishop
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf