Author Topic: C function like printf but not sending to stdout? (XC8)  (Read 30410 times)

0 Members and 1 Guest are viewing this topic.

Offline David_AVD

  • Super Contributor
  • ***
  • Posts: 2607
  • Country: au
Re: C function like printf but not sending to stdout? (XC8)
« Reply #100 on: September 30, 2013, 06:52:44 am »
The warning I get with that is: "incompatible implicit declaration of built-in function 'sprintf' [enabled by default]".

I should mention this is with AVR GCC, not XC8.  In hindsight I should have started a new thread.

The code is:
Code: [Select]
sprintf(Buttons[BtnExtTime].Caption,"%u2.2:%u2.2",(unsigned int)mins,(unsigned int)secs);
 

Offline amyk

  • Super Contributor
  • ***
  • Posts: 6541
Re: C function like printf but not sending to stdout? (XC8)
« Reply #101 on: September 30, 2013, 07:29:00 am »
What is the type of Buttons[BtnExtTime].Caption ? sprintf is declared as taking something like (char*, char *, ...)

It's a variadic function - that means it can take a variable number of parameters, and in this case the 2nd one - the format string - gives meaning on how to interpret the parameters after it.

Also your format string should be %2.2u and not %u2.2 - the letter ends the conversion specifier so you're going to get e.g. "122.2:52.2" instead of "12: 2".
 

Offline IanB

  • Super Contributor
  • ***
  • Posts: 9593
  • Country: us
Re: C function like printf but not sending to stdout? (XC8)
« Reply #102 on: September 30, 2013, 07:35:07 am »
instead of "12: 2"

Just FYI, a format specifier of %2.2u should print 2 as "02" with a leading zero. With that format you get a field width of 2 with 2 digits printed.
I'm not an EE--what am I doing here?
 

Offline Galaxyrise

  • Frequent Contributor
  • **
  • Posts: 526
  • Country: us
Re: C function like printf but not sending to stdout? (XC8)
« Reply #103 on: September 30, 2013, 08:20:14 am »
%02u will also give you at least two digits, 0-prefixed if needed.  I like doing it that way since that's also how you 0-prefix floats.

printf-style formatting does take some getting used to, but it's quite powerful and as such has been implemented in quite a few languages now. 

"incompatible implicit declaration of built-in function 'sprintf' [enabled by default]".
The compiler encountered a call to sprintf without being told what sprintf would look like.  That's normally ok because implicit declarations are enabled, but the compiler has knowledge of sprintf built in and that built-in prototype isn't compatible with the arguments you're passing it. So the compiler hopes you know what you're doing.  I would guess that warning means the built-in behavior won't run, and instead a call to a real function "sprintf" will be generated.

I'm not sure what the implicit declaration of sprintf looks like, but it doesn't have to be implicit.  To get the explicit declaration of sprintf, #include <string.h> Once the compiler has an explicit declaration, the warning will likely turn into an error (something like "could not convert parameter 2 from char const * to char *")
« Last Edit: September 30, 2013, 08:22:49 am by Galaxyrise »
I am but an egg
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8229
  • Country: 00
Re: C function like printf but not sending to stdout? (XC8)
« Reply #104 on: September 30, 2013, 10:27:48 am »
Quote
The warning I get with that is: "incompatible implicit declaration of built-in function 'sprintf' [enabled by default]".

Include the header file where sprintf() is prototyped.
================================
https://dannyelectronics.wordpress.com/
 

Offline David_AVD

  • Super Contributor
  • ***
  • Posts: 2607
  • Country: au
Re: C function like printf but not sending to stdout? (XC8)
« Reply #105 on: October 01, 2013, 12:50:32 am »
Thanks again guys.  Those forgotten include files will be the death of me.   |O

The following is what I ended up with and it works well.

Code: [Select]
unsigned int mins, secs;
mins = ExtendedTime / 60;
secs = ExtendedTime % 60;
sprintf(Buttons[BtnExtTime].Caption,"%2.2d:%2.2d",mins,secs);
 

Offline ve7xen

  • Frequent Contributor
  • **
  • Posts: 671
  • Country: ca
    • VE7XEN Blog
Re: C function like printf but not sending to stdout? (XC8)
« Reply #106 on: October 02, 2013, 05:30:43 am »
This particular string will always be 5 characters long, so you could just generate it with assignment:
Code: [Select]
Buttons[BtnExtTime].Caption[0] = '0' + (ExtendedTime / 60) / 10;
Buttons[BtnExtTime].Caption[1] = '0' + (ExtendedTime / 60) % 10;
Buttons[BtnExtTime].Caption[2] = ':';
Buttons[BtnExtTime].Caption[3] = '0' + (ExtendedTime % 60) / 10;
Buttons[BtnExtTime].Caption[4] = '0' + (ExtendedTime % 60) % 10;
Buttons[BtnExtTime].Caption[5] = '\0'; // if necessary
73 de VE7XEN
 

Offline David_AVD

  • Super Contributor
  • ***
  • Posts: 2607
  • Country: au
Re: C function like printf but not sending to stdout? (XC8)
« Reply #107 on: October 02, 2013, 06:47:11 am »
This particular string will always be 5 characters long, so you could just generate it with assignment:

That's pretty much exactly how I did it before asking.  I did want to learn how to do it the sprintf way for other uses though.
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8229
  • Country: 00
Re: C function like printf but not sending to stdout? (XC8)
« Reply #108 on: October 02, 2013, 10:11:48 am »
Generally, printf is costly in speed and execution and should be avoided, unless you are already using it.

My preference is to use itoa() or ltoa() or their variants - they are implemented on most compilers and even if they aren't, they are simple to write your own.

Be careful however that some implementations of itoa() / ltoa() are done through sprintf().
================================
https://dannyelectronics.wordpress.com/
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf