Shop OBEX P1 Docs P2 Docs Learn Events
Strange problem with printf — Parallax Forums

Strange problem with printf

Hal AlbachHal Albach Posts: 747
edited 2013-08-26 11:24 in Learn with BlocklyProp
I'm going through the Learn C lessons and on the Lesson which uses the program "Decide on Multiple Conditions.c" I have run into a problem with the printf function. In the program there are three terminal output statements that actually use the command print(.....). In listening to tymkrs podcasts Roy has made mention to the effect that print(....) works with the GCC compiler but the proper command to use would be printf(.....). So I changed the three print to printf and even though no errors and the code compiled and loaded to ram, there was no output to the terminal screen. If I changed any of the three printf to print then the three output lines would appear. Didn't matter which statement was changed, as long as one of the three output statements used print instead of printf, the output would appear. I have two Propeller Project Boards, one with the normal 5MHz crystal and the other with a 6MHz external oscillator. This printf thing happens on both boards. (Yes, I made adjustments to the board config file when using the 6MHz board.) I guess my question would be to use only print statements and not use printf at all or is there a possible problem with the compiler?

Comments

  • tdlivingstdlivings Posts: 437
    edited 2013-08-23 08:18
    Hal
    print(...) is part of the SimpleText lib which contains functions to write to the default SimpleIDE terminal.
    Those are the functions at the top of the list that do not take a pointer to a text_t device.
    The other functions which take a pointer to a text_t device could be to fdserial for full duplex serial to talk to an XBEE
    or the Parallax Serial Terminal another text_t device is serial which is simple serial to send text to say a serial LCD.


    I am guessing Roy was thinking it was a typo and should be printf but it is not.
    It looks like when you include one print(..) you are indirectly telling printf(..) how to
    talk to the default terminal in SimpleIDE.

    Tom
  • Hal AlbachHal Albach Posts: 747
    edited 2013-08-23 08:49
    Thank you for clearing that up. For now when working with SimpleIDE I will just use the print(...) statement. Is there a listing of commands readily available that differentiate between ANSI C and GCC used by SimpleIDE? I currently refer to my K & R ANSI C reference book and would like to know when it is and isn't relevant.
  • Steph LindsaySteph Lindsay Posts: 767
    edited 2013-08-23 11:44
    Both print from the Propeller C Education's simpletext library, and printf from PropGCC's implementation of the stdio library, can be used from SimpleIDE.

    We have made life a bit more complex for you intrepid early adopters, because Andy's first release of the Propeller C Tutorials had a simpletools library that called PropGCC's stdio library, and used printf. The first several Tymkrs podcasts referenced printf because that's what our posted tutorials were using at the time.

    We belatedly found that some combinations of function calls (Andy would remember which, perhaps it was printf and scanf, with floating point?) caused the compiled code size to get huge rapidly. So, Andy and Steve redesigned simpletools to reference the new simpletext and fdserial libraries that keep the code compact. The updated libraries were released with SimpleIDE 0.9.40 and the Propeller C tutorials were revised at the same time.

    However, we did archive the older tutorials and libraries, which are available online. These should be compatible with the first Tymkrs podcasts.

    http://learn.parallax.com/C/propeller-c-tutorial-archive

    For seeing how PropGCC and its implementation of standard libraries compares to other ones out there, you might try https://sites.google.com/site/propellergcc.

    To learn more about the Propeller C Tutorial's Simple Libraries, you can click SimpleIDE's Help and select the Simple Library Index. It's a start - we have much more to do regarding our library documentation.

    One last note - if you install the latest version of SimpleIDE, it will not overwrite the Documents/SimpleIDE folder you would already have on your computer. If you simply rename any previous Documents/SimpleIDE folder, then close and re-open SimpleIDE 0.9.40, it will create a new Documents/SimpleIDE folder stocked with the updated Learn folder full of Simple Libraries and example code for the Propeller C Tutorials.

    I apologize the for the confusion we created!
  • jazzedjazzed Posts: 11,803
    edited 2013-08-23 17:49
    Hi.

    The propeller-gcc printf is ANSI-C compliant as are all the other functions that are required by ANSI-C stand-alone libraries. We went to great lengths to provide smaller though still compliant versions of the functions. Even with that effort, the libraries can still be rather large. Optional Simple printf and Tiny Lib printf are not ANSI-C compliant.

    A program with the current ANSI-C printf when using 32-bit doubles easily approaches 27KB in some modes and needs the math library linked properly function. That leaves practically no room for the application! We provided XMM and XMMC solutions for users who demand being able to create full solutions using only ANSI-C. Parallax wanted a micro-controller sized solution, which means no XMM stuff. Simple Libraries provide what Parallax wants.

    Simple Libraries provides micro-controller sized printing with putStr, putHex, putDec, putBin, putln (or putLine) and other output functions. Using those functions, a CMM program can easily be less than 4KB depending on your needs (2KB of that is the C kernel - Spin also has one of those, but it lives in Propeller ROM).

    Andy and I came up with the Simple Library "print" solution. It will do 32-bit doubles without including the math library and take around 4KB of code in CMM mode (a Hello program will be 6KB because the C kernel as mentioned above). The disadvantage of print is that it's always larger than a simple putLine, but it's always the same size regardless of what you use it for .. it will never grow by adding %d, %f, etc.... It's predictable.

    We struggled about whether or not our print function should be called printf.

    I argued that printf is an ANSI-C name, and that it should be ANSI-C compliant. There have been other printf solutions that were not compliant and smaller in some cases, but caused much grief. because people had certain expectations, and the solutions (I.E. TinyLib) require global device settings and could not easily support different devices. Basically calling a print formatting function printf that didn't live up to the expectations of the name was just a disaster, so we decided to use just print.

    All of the functions and usage provided by Simple Libraries and simpletext.h (the simple text input/output library) are in the documentation provided in each Simple Library and Workspace distribution. It is also on-line https://propsideworkspace.googlecode.com/hg-history/029efb9bf270758003c0a67571d1cbe4cc9986b5/Learn/Simple%20Libraries%20Index.html.

    ANSI-C functions are also described on line and pointers to the fucntion descriptions and usage can be found in the Libraries section of the parallax propeller-gcc site .https://sites.google.com/site/propellergcc/documentation/libraries

    I'm sorry for any confusion caused by Tymkrs First-C with all this. The lack of coordination and some other things bother me, but it's an independent Podcast show created by the authors that has it's own trajectory and goals. Thank goodness they have Roy to be a technical anchor at least.

    Best regards to all.
    --Steve
  • Hal AlbachHal Albach Posts: 747
    edited 2013-08-24 12:13
    Steve, Steph, & Tom,
    Thank you for the extremely informative responses. I had forgotten just how huge printf() is. (Its been a while since I played with C). As I indicated in my response to Tom's post, and with the wealth of information from Steph and Steve, I am more than happy to use the print() implementation provided by Steve, particularly since it does everything I need to do. Just as a matter of curiosity, though, is there an additional condition that has to be met when I, for unknown devious reasons, may want to torture myself and use printf() or puts(), because neither statement will output to the terminal screen until a print() statement has been included in the program. As Tom indicated, the print() statement is somehow indirectly letting printf() and puts() know how to talk to the default terminal in SimpleIDE.
    I just wish I was a little more knowledgeable with C's inner workings and not have to resort to asking what very well could be a very dumb question. :innocent:

    Gratefully,
    Hal
  • jazzedjazzed Posts: 11,803
    edited 2013-08-24 14:50
    Hal Albach wrote: »
    As Tom indicated, the print() statement is somehow indirectly letting printf() and puts() know how to talk to the default terminal in SimpleIDE.

    The printf() is printing before the Simple Terminal starts. Add this statement before printf() ...

    waitcnt(CLKFREQ/2+CNT);

    That will delay the output long enough for you to see it on the terminal.
    There is also a pause() function in the simple library ....
  • Roy ElthamRoy Eltham Posts: 2,998
    edited 2013-08-24 18:01
    I really don't like the idea of using print() in place of printf(). It should be a function name that is something more clearly special to the Prop stuff, especially for education where you'll be teaching folks print() and they'll go off and use C on other platforms and print() won't work at all.

    In fact, I think it would be better to just have it be printf() that doesn't have as much capability depending on which libs you compile/link with.
  • jazzedjazzed Posts: 11,803
    edited 2013-08-26 11:24
    Roy Eltham wrote: »
    In fact, I think it would be better to just have it be printf() that doesn't have as much capability depending on which libs you compile/link with.

    This is all up to Parallax.

    IMHO, I see no value in confusing what is or is not possible with ANSI-C printf(). For example while print formatting via printf() has many options, Parallax required %b, and it is implemented in print(). Format specifier %b is not ANSI-C compliant. Other format specifiers which are required in ANSI-C printf() are not included in print(). Parallax is providing a practical MCU sized solution with Simple Libraries. A full and practical ANSI-C compliant stand alone solution is also provided by Propeller-GCC for those who need it.
Sign In or Register to comment.