I have programmed a lot in C and C++, not on embedded controllers but on unix machines writing servers and drivers. One of the functions I used a lot was the `printf` family:
char temp[40]; float aFloat = 10.0 int anInt = 5 char *str = "a test string"; sprintf (temp, "%f %d %s", aFloat, anInt, str);
I notice that in a lot of sketches two or even three Serial.print and Serial.println are used to convey some information to the console. Is there a reason the printf family is not used?
RonS
I have programmed a lot in C and C++, not on embedded controllers but on unix machines writing servers and drivers. One of the functions I used a lot was the `printf` family:
char temp[40]; float aFloat = 10.0 int anInt = 5 char *str = "a test string"; sprintf (temp, "%f %d %s", aFloat, anInt, str);I notice that in a lot of sketches two or even three Serial.print and Serial.println are used to convey some information to the console. Is there a reason the printf family is not used?
RonS
sprintf works, but printf is not technically implemented.
As I mentioned elsewhere... wish the implementers would include more modern C and C++ libraries, but I think they are all too worried about memory space... hard to agree with, especially when many embedded devices these days have far grater memory available... Perhaps it's about time this kind of thinking changed?
Cheers.
Haven't had a need to so far, and I think almost anything I will be doing in this microcontroller/microcomputer environment will all be browser/cloud output in any case.
Arduino says and I agree, in general, the const keyword is preferred for defining constants and should be used instead of #define
"Never wrestle with a pig....the pig loves it and you end up covered in mud..." anon
My experience hours are >75,000 and I stopped counting in 2004.
Major Languages - 360 Macro Assembler, Intel Assembler, PLI/1, Pascal, C plus numerous job control and scripting
I think it's probably to save memory. I know next to nothing about embedded programming, but there's also nowhere useful for the output of printf to go. Embedded devices don't have a display (e.g. monitor) or console, so any debug output has to be communicated back to a computer that can do something with the output.
Have you ever looked at the amount of machine code created by one of these statements, and the amount of libraries needed to support it. Not a good fit for micro's. We aren't printing to 1403N1's and followers after all.
Arduino says and I agree, in general, the const keyword is preferred for defining constants and should be used instead of #define
"Never wrestle with a pig....the pig loves it and you end up covered in mud..." anon
My experience hours are >75,000 and I stopped counting in 2004.
Major Languages - 360 Macro Assembler, Intel Assembler, PLI/1, Pascal, C plus numerous job control and scripting
@yurkshirelad I think a web page could be an output device, but that would probably be best implemented via sprintf statements. Still has all the 'power' of printf but no device I/O just a string. That fits well with html, I can even imagine an HTML specific variant, hmmmm, another project????
Arduino says and I agree, in general, the const keyword is preferred for defining constants and should be used instead of #define
"Never wrestle with a pig....the pig loves it and you end up covered in mud..." anon
My experience hours are >75,000 and I stopped counting in 2004.
Major Languages - 360 Macro Assembler, Intel Assembler, PLI/1, Pascal, C plus numerous job control and scripting
I supposed you could write the debug output to a web page, but it would need WiFi support, and you'd obviously need to run a web server on the embedded device. It would also need some storage for the HTML page, so you can output lots of debug statements. Would a custom serial connector be a better solution? You could then run Putty (Window app) and connect your PC to the device and watch the output of the serial connection.
I notice that in a lot of sketches two or even three Serial.print and Serial.println are used to convey some information to the console. Is there a reason the printf family is not used?
There is a library for sprint() ...
I had a psychic girlfriend but she left me before we met.
@yurkshirelad Sorry, a lot of assumptions on both sides. Since Bill introduced us to Arduino Cloud, most of my interest/assumptions/etc is now cloud based. MY BAD. Unless I am very mistaken (VERY possible) you can sprintf with all the fancy abilities, then choose to just pump it out the serial port. I have yet to encounter a need for that, but I think it's very possible one may find a need some day.
Arduino says and I agree, in general, the const keyword is preferred for defining constants and should be used instead of #define
"Never wrestle with a pig....the pig loves it and you end up covered in mud..." anon
My experience hours are >75,000 and I stopped counting in 2004.
Major Languages - 360 Macro Assembler, Intel Assembler, PLI/1, Pascal, C plus numerous job control and scripting
@will I saw that, filed away under 'if I ever need any fancy output this is one way to do it'
Arduino says and I agree, in general, the const keyword is preferred for defining constants and should be used instead of #define
"Never wrestle with a pig....the pig loves it and you end up covered in mud..." anon
My experience hours are >75,000 and I stopped counting in 2004.
Major Languages - 360 Macro Assembler, Intel Assembler, PLI/1, Pascal, C plus numerous job control and scripting
@yurkshirelad Sorry, a lot of assumptions on both sides. Since Bill introduced us to Arduino Cloud, most of my interest/assumptions/etc is now cloud based. MY BAD. Unless I am very mistaken (VERY possible) you can sprintf with all the fancy abilities, then choose to just pump it out the serial port. I have yet to encounter a need for that, but I think it's very possible one may find a need some day.
No worries. I believe sprintf simply formats a string and has no relation to output, so it's still very useful.
@yurkshirelad Absolutely, I am thinking of using sprintf to build a line of html code to display on a webpage. It will be cool if I can figure out colors, bold, underline etc, I think it's possible that is in sprintf, but it has been several decades since I last used it. Just did a quick google, and the answer is YES
https://www.theurbanpenguin.com/4184-2/
Arduino says and I agree, in general, the const keyword is preferred for defining constants and should be used instead of #define
"Never wrestle with a pig....the pig loves it and you end up covered in mud..." anon
My experience hours are >75,000 and I stopped counting in 2004.
Major Languages - 360 Macro Assembler, Intel Assembler, PLI/1, Pascal, C plus numerous job control and scripting
@yurkshirelad Absolutely, I am thinking of using sprintf to build a line of html code to display on a webpage. It will be cool if I can figure out colors, bold, underline etc, I think it's possible that is in sprintf, but it has been several decades since I last used it. Just did a quick google, and the answer is YES
https://www.theurbanpenguin.com/4184-2/
If you're building html, just include some css styling in the header. Or add styling to each html element. It's pretty easy.
@yurkshirelad Sorry, no training and I am probably using incorrect terminology anyway. I am fairly confident for what I am going to be doing that a sprintf statement aimed at a text field on a page will work.
Arduino says and I agree, in general, the const keyword is preferred for defining constants and should be used instead of #define
"Never wrestle with a pig....the pig loves it and you end up covered in mud..." anon
My experience hours are >75,000 and I stopped counting in 2004.
Major Languages - 360 Macro Assembler, Intel Assembler, PLI/1, Pascal, C plus numerous job control and scripting
That link you posted isn't about generating html. I think, and this makes more sense, it's about formatting text for display in something like a Linux shell. Interesting, insidnt realize it was that easy. So thanks for the link.