Shop OBEX P1 Docs P2 Docs Learn Events
Confusion in C — Parallax Forums

Confusion in C

ajwardajward Posts: 1,130
edited 2015-02-15 14:09 in Learn with BlocklyProp
Hi All...
Another question, but first my usual reaction to coding in C.

Charliebrown-1-.jpg


I've come across these guys: scan and scanf also print and printf. What difference does the "f" make? They "seem" to do the same thing. The Great InterWebs isn't any help here. All I can find are references to scanf and printf.

Thanks for any advice!

Amanda
359 x 450 - 31K

Comments

  • David BetzDavid Betz Posts: 14,516
    edited 2015-02-10 04:07
    ajward wrote: »
    Hi All...
    Another question, but first my usual reaction to coding in C.

    Charliebrown-1-.jpg


    I've come across these guys: scan and scanf also print and printf. What difference does the "f" make? They "seem" to do the same thing. The Great InterWebs isn't any help here. All I can find are references to scanf and printf.

    Thanks for any advice!

    Amanda
    printf and scanf are the standard C library functions that any C compiler will support.
    print and scan are versions invented by Parallax that in most cases will use less memory.
  • Dave HeinDave Hein Posts: 6,347
    edited 2015-02-10 05:53
    I think it would be better to do something like the _simple_printf trick, where a checkbox is used to generate a -D option to change all the printf's to _simple_printf. I'm not sure what the difference is between print and _simple_printf, but we probably only need one of them. If the -D option is used then print and scan should be change to something more unique like _simple_printf and _simple_scanf.
  • David BetzDavid Betz Posts: 14,516
    edited 2015-02-10 05:58
    Dave Hein wrote: »
    I think it would be better to do something like the _simple_printf trick, where a checkbox is used to generate a -D option to change all the printf's to _simple_printf. I'm not sure what the difference is between print and _simple_printf, but we probably only need one of them. If the -D option is used then print and scan should be change to something more unique like _simple_printf and _simple_scanf.
    I agree. Not sure why they didn't use simple_printf and simple_scanf for the Learn stuff. I think it may have had to do with floating point support being larger in the simple_xxx variants.
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2015-02-10 07:36
    scanf and printf, the original C library functions were very ambitious. They were developed for larger, faster computers tied into serial interfaces, and bog down on small microcontrollers.... just too many format options filtering input and output.

    I too have had trouble with sorting out the names and _simple_printf might be a way to make this all go away.

    After trying to be a diligent ANSI C student and study all and everything in the ANSI C, 2nd ed., I just had to accept that all the discussion of scanf and printf were mostly legacy material. On the one hand, it bogs down small microcontrollers trying to use a serial port. On the other hand, big computers tend to do all and everything in GUI.

    Fully mastering scanf and printf might just be a waste of time.
  • Dave HeinDave Hein Posts: 6,347
    edited 2015-02-10 08:10
    Fully mastering scanf and printf might just be a waste of time.
    I think the 90/10 rule applies, or maybe it's 80/20. In 20% of the time you can learn 80% of scanf and printf. You can do a lot without knowing the remaining 20%, except when you encounter it in somebody else's code. And then it just takes a few minutes to look up the specific feature that was used in that code.
  • LeonLeon Posts: 7,620
    edited 2015-02-10 08:45
    It's 80/20, and is called the Pareto principle:

    http://en.wikipedia.org/wiki/Pareto_principle
  • NWCCTVNWCCTV Posts: 3,629
    edited 2015-02-10 16:29
    Goes to show how much I know. For some reason I thought scan and print were for C and the added f was for C++. Anyone ever use Borland C++? That is where I got me feet wet in programming.
  • David BetzDavid Betz Posts: 14,516
    edited 2015-02-10 18:36
    jazzed wrote: »
    Every now and then I read these forums. Some things interest me more than others. Here's my take:

    The stdio overhead contributes much of of printf and friends bloating. Then you add floating point (needing to link libm for anything to actually work), and suddenly there is hardly any memory left for the rest of your program.

    The other problem is in how to use more than one serial port (or other device for example). The default way that propeller-gcc allows for this is very cumbersome. It's much easier to just define the pins being used when the device is initialized.

    That being said, if you only want to print a string, printf and friends can be quite small.

    All of the Propeller C functions like print and friends have simple documentation in the SimpleIDE package and other Parallax resources.

    IMHO, for anyone who wants to use ALL the features of the C language, then choose a different processor (or at least an MCU with enough memory) for the job. Regardless of the language used, if the scope of the project is too big, then you will end up with a failure. Constantly having to worry about memory is a burden that you don't really need.
    None of this explains why the simpler versions need to have different names. You could have implemented a subset of the full printf/scanf and called them by the same names. I think you did implement a subset but for some reason chose different names. In fact, I very seldom use any printf formats other than %s and %d and maybe %x. A subset would be perfectly fine for most purposes and has the advantage that you can then take that code and compile it to run on any system that has a full printf without any changes.
  • User NameUser Name Posts: 1,451
    edited 2015-02-10 19:44
    Reminds me of my first incursion into the ARM Cortex M0 world. I ignorantly used sprintf() to do base conversions and discovered that 30k of a 32k device just got eaten up.

    So I wrote a simple base conversion routine which turned out to run 3x faster and take 1/10 the space.
  • Dave HeinDave Hein Posts: 6,347
    edited 2015-02-11 07:05
    David Betz wrote: »
    None of this explains why the simpler versions need to have different names. You could have implemented a subset of the full printf/scanf and called them by the same names.
    Isn't that the idea behind the tiny library? There's a check box under the linker tab that allows selecting "Tiny lib". Of course, first you have to enable the project view to get to it. I wish the setting for the project view would persist in SimpleIDE, but I have to re-enable it every time I start it up.

    It's very confusing having print/scan, _simple_printf and the tiny lib. It seems like the tiny lib approach would be the easiest one for newbies and experienced programmers.
  • ajwardajward Posts: 1,130
    edited 2015-02-15 13:26
    Thanks everyone for the prompt and helpful responses!

    Amanda
  • David BetzDavid Betz Posts: 14,516
    edited 2015-02-15 13:31
    jazzed wrote: »
    The simpletext functions are not at all stdio compliant. I'd rather see C programmers complain about the names than trip over the "unexpected" behavior ....
    They aren't just a subset of the standard printf and scanf functions?
  • ajwardajward Posts: 1,130
    edited 2015-02-15 13:47
    I thought Propeller C was a subset.

    @
  • David BetzDavid Betz Posts: 14,516
    edited 2015-02-15 14:09
    ajward wrote: »
    I thought Propeller C was a subset.

    @
    I assume that "Propeller C" means the port of GCC to the Propeller that is in the Google Code propgcc repository. That it is a full implementation of C no a subset.
Sign In or Register to comment.