If you don’t learn sprintf(), your code will hate you later

If you don’t learn sprintf(), your code will hate you later

Programming Electronics Academy

3 года назад

52,175 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@John-qe3ky
@John-qe3ky - 05.06.2021 02:08

Very nicely explained, please continue with the additional functions.

Ответить
@DodgyBrothersEngineering
@DodgyBrothersEngineering - 06.06.2021 02:50

This video would have come in real handy just the other day, but better late than never... Would have been nice if you covered the use of the \ as in \"%s\".

Ответить
@_GB84
@_GB84 - 07.06.2021 22:39

Brilliant! Great info. I have been struggling with why it wouldn't print %.2f for a large majority of the day and now I know :D, thanks. You mention you have another vid explaining 'dtostrf' but I can't seem to find it. Can you point me in the right direction please? Thanks for your time and effort with your videos, they're super clear!

Ответить
@iamsparkicus
@iamsparkicus - 08.06.2021 10:58

Cheers Fella,
you are an amazing teacher. Love your channel. I am starting to understand things I thought were beyond my intelect!
Keep 'em coming!

Ответить
@bobowen7861
@bobowen7861 - 19.06.2021 21:09

Very excellent tutorial. You made this simple. Would be very interested in seeing an additional tutorial on using optional sub-specifiers.

Ответить
@banditboy6444
@banditboy6444 - 06.07.2021 21:02

As always, great vid

Ответить
@ingenierocristian
@ingenierocristian - 07.07.2021 02:28

Love it, thanks

Ответить
@TOMTOM-nh3nl
@TOMTOM-nh3nl - 14.10.2021 08:10

Thank You

Ответить
@JPN76
@JPN76 - 17.10.2021 13:00

Is it possible to use this to send multiple variable data to another arduino? If so how would you separate it on that side and plug the data into the correct variable on the recieving arduino?

Ответить
@ElIngLopez
@ElIngLopez - 26.10.2021 10:00

Great! ... I'd like to print the % sign, how i do this?

Ответить
@Clip7heApex
@Clip7heApex - 20.11.2021 02:48

Would this work for LCD print?

Ответить
@steventhehistorian
@steventhehistorian - 04.12.2021 07:51

You have the best Arduino function tutorials. Thank you so much for this content.

Ответить
@madvillage6478
@madvillage6478 - 31.12.2021 02:14

I just use:
Serial.println("The " + String(numBurritos) + " burritos are " + String(tempStr) + " degrees F.");

Ответить
@bibel2k
@bibel2k - 01.01.2022 10:04

I'm ‏speechless!
One of the most annoying things on arduino is to print a long message like you have shown.

Mind blowing!

Ответить
@xroppa5290
@xroppa5290 - 01.01.2022 21:17

Top

Ответить
@sombatmuenphan1555
@sombatmuenphan1555 - 26.01.2022 17:58

Many thank.Excellent explain.

Ответить
@markadyash
@markadyash - 01.02.2022 22:47

thanks lot helpful

Ответить
@MN-be7sx
@MN-be7sx - 18.02.2022 08:39

please make very detailed videos series on sprintf and sub-specifiers

Ответить
@user-su5sq5ib3i
@user-su5sq5ib3i - 21.03.2022 04:52

Nice explanation just what I hope will work with my microchip pic18f and C language. I'm doing a weather station

Ответить
@MrShanmunir
@MrShanmunir - 21.03.2022 17:08

great man. Really good explanation.. please do a follow up. Thx

Ответить
@dalwinderssi4094
@dalwinderssi4094 - 30.03.2022 15:31

Yes , I want to buy ,lessions

Ответить
@vikky452
@vikky452 - 07.04.2022 20:38

thank you sir, very good tutorial. plz make such wonderful tutorial.

Ответить
@dalwinderssi4094
@dalwinderssi4094 - 09.04.2022 16:07

Want know how to read from sd card and print on tft display using arduino uno

Ответить
@electronics5449
@electronics5449 - 05.05.2022 11:50

Awesome

Ответить
@mtraven23
@mtraven23 - 29.05.2022 07:15

a good explanation...but thats still cumbersome. I prefer using the "Streaming.h" library. this allows for c++ style prints:

Serial << "hello world, I am " << intVar1 << " years old" << endl;

you can add as many as you like. my test indicated in requires no additional clock cyles to the native print.
"endl" is a new line, if you dont want it, just leave it off.

Ответить
@giorgito3313
@giorgito3313 - 02.07.2022 10:25

Excellent!!

Ответить
@joshuapitong899
@joshuapitong899 - 20.07.2022 08:53

Really great.✔️ Thanks.🙌

Ответить
@jslonisch
@jslonisch - 23.07.2022 04:15

Isn’t it easier just to do it all in one line with Serial.println(String(variable1 + variable2 + ...)). No messing around with a buffer or worrying about if you got the right character specifier to suit your variable.

Ответить
@fernandosimonetti6595
@fernandosimonetti6595 - 26.07.2022 05:17

Excelente explicación! Muchas gracias!!!

Ответить
@DavidRisnik
@DavidRisnik - 29.09.2022 13:40

congratulations for the class, very explanatory. I'm doing a project using a panel of leds-dot matrix to print texts read by the SD card. How can I include the temperature value read by a sensor (DB1820), in the text sent by reading the SD card? thank you if you can clarify this doubt for me

Ответить
@christiaansteyn4088
@christiaansteyn4088 - 01.01.2023 22:59

Please do the additional tutorials

Ответить
@rossli8621
@rossli8621 - 02.01.2023 11:51

Very nice video!!!

Ответить
@12DGJB21
@12DGJB21 - 19.01.2023 21:09

Thank you, sprintf looks very useful. Would there be any benefit of using sprintf over the following single line command to print a simple string of text with a variable (i)?
For example: Serial.println((String)"Relay"+(i+1)+" OFF"); Where the variable i is a numerical value starting at 0.

Ответить
@ED99LAM
@ED99LAM - 03.02.2023 15:07

very useful n informative

Ответить
@ericBcreator
@ericBcreator - 15.02.2023 16:55

It is convenient and tidy but keep in mind this method is (a lot) slower than using a bunch of Serial.print commands, as I found out recently when I tried to clean up my new VU meter project code.

Ответить
@semibiotic
@semibiotic - 09.03.2023 08:08

It should be snprintf().

Ответить
@TheMarbo74
@TheMarbo74 - 20.03.2023 00:43

Where has sprintf been all my life?
Better question is why don't I see it on the Arduino reference website and how many more hidden jewels are out there?

Ответить
@leyasep5919
@leyasep5919 - 27.03.2023 16:50

And this, people, is how a new generation is trained to enable buffer overflows and vulnerabilities...
Don't the authors know that sprintf is a deprecated and dangerous function ?
Nah, who cares.

Ответить
@berndeckenfels
@berndeckenfels - 14.05.2023 17:53

The function is called snprintf

Ответить
@NotMarkKnopfler
@NotMarkKnopfler - 16.05.2023 11:02

Everything said in this video is correct. However, a couple of things to bear in mind: sprintf is a LOT slower than Serial.print. Also, sprintf uses a lot of memory. On things like an Arduino Uno, Nano, or ATTINY85 you may struggle. What I do, to keep my code tidy, is write a function to display the things I want to display using Serial.print. You don't save on lines of code, but you can move those pesky Serial.print lines - which tend to clutter up the code - into their own functions. Which is much neater. It's also easier to remove them when you don't need them any more. So, using the burritos example from the video:

void displayBurritosInfo(int numberOfBurritos, float temperature)
{
Serial.print("The temperature of the ");
Serial.print(numberOfBurritos);
Serial.print(" is ");
Serial.println(temperature);
}

Ответить
@crissd8283
@crissd8283 - 17.05.2023 04:38

This seems inefficient. While the first example may have more lines of code, the second code certainly uses more resources. Using a character array requires the same data to be stored twice. Once in the program and variables and a second time in the character array. Then you are spending time sending that data into that memory location then sending that new memory location to print(). The first example is certainly faster and more efficient.

I understand that neat looking code is desired but the second example is harder to understand and less efficient. Seems like a bad idea.

Ответить
@redcrafterlppa303
@redcrafterlppa303 - 17.05.2023 11:35

This is potentially creating a buffer overrun. As it depends on how long the inserted numbers/strings are. When inserting strings you can never make the buffer big enough since you don't know the length of the dynamic strings at compile time.
Meaning you would need to allocate the buffer on the heap wasting already limited heap memory on an arduino.

Also this whole thing is just unnecessary. There is nothing wrong with multiple Serial print statements. In fact it's more efficient.
There are cases where sprintf makes sense to use. But this isn't one.

Ответить
@jason_man
@jason_man - 02.01.2024 00:03

just use String() and + this point

Ответить
@lesatkins42
@lesatkins42 - 20.01.2024 23:14

or you could use Serial.printf(" .... ", var1, var2, ... varn);

Ответить
@livetohash6152
@livetohash6152 - 18.04.2024 03:13

Can we use $"The taco is {tempTaco} degrees"
?

Ответить
@PraveenChintha-j2t
@PraveenChintha-j2t - 30.07.2024 17:42

Sir, actually no need to use sprintf rather we can directly use the string concatenation property in serial.print() function

Ответить
@clems6989
@clems6989 - 30.07.2024 23:06

The website will not send me the email needed for signup !!

Ответить
@johnnz4375
@johnnz4375 - 02.10.2024 12:33

This is so much clearer than the online Arduino reference that is obviously written by well seasoned programmers and assume that you have solid grasp of the C language.

Ответить