Notifications
Clear all

What happened with `printf` and its family?

39 Posts
6 Users
9 Likes
2,445 Views
(@ronsmitsnl)
Member
Joined: 2 years ago
Posts: 20
Topic starter  

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


   
Biny reacted
Quote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@ronsmitsnl

Posted by: @ronsmitsnl

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.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6898
 

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.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
(@yurkshirelad)
Member
Joined: 3 years ago
Posts: 493
 

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.


   
Ron reacted
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6898
 

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.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6898
 

@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???? 

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
(@yurkshirelad)
Member
Joined: 3 years ago
Posts: 493
 

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.


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2507
 
Posted by: @ronsmitsnl

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() ...

https://www.arduino.cc/reference/en/libraries/libprintf/

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6898
 

@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.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6898
 

@will I saw that, filed away under 'if I ever need any fancy output this is one way to do it'

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
(@yurkshirelad)
Member
Joined: 3 years ago
Posts: 493
 
Posted by: @zander

@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.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6898
 

@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/

 

 

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
(@yurkshirelad)
Member
Joined: 3 years ago
Posts: 493
 
Posted by: @zander

@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.


   
frogandtoad reacted
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6898
 

@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.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
(@yurkshirelad)
Member
Joined: 3 years ago
Posts: 493
 

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.


   
ReplyQuote
Page 1 / 3