View previous topic :: View next topic |
Author |
Message |
SeeCwriter
Joined: 18 Nov 2013 Posts: 160
|
Format error |
Posted: Tue May 28, 2019 12:47 pm |
|
|
In the function below, I get an invalid format error for "mday". If I remove the first element, leaving only "month" and "mday" it compiles without error. Is there something stupid I've done? Compiler v5.085.
Code: |
void Test()
{
char mday[3], result[12];
int month = 5;
long yr = 2019;
strcpy( mday, "28" );
sprintf( result, "%L-%d-%s", yr, month, mday );
}
|
|
|
|
newguy
Joined: 24 Jun 2004 Posts: 1909
|
|
Posted: Tue May 28, 2019 1:16 pm |
|
|
Instead of strcpy, try sprintf instead just to see if that makes a difference. |
|
|
dluu13
Joined: 28 Sep 2018 Posts: 395 Location: Toronto, ON
|
|
Posted: Tue May 28, 2019 1:19 pm |
|
|
The only thing that I can tell is that I think %L is wrong.
In the help file, only %lu, %ld, %lx and %lX are stated as format specifiers with l. I would use %lu.
And for year, I would use uint16_t instead of long... Always good to explicitly state the size. For month, I would use uint8_t. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Tue May 28, 2019 2:10 pm |
|
|
OK, I'm old, tired, wet, and beatup.. but I have to ask WHY have the day of the month as a string when printed? Isn't it reall an unsigned 8 bit value ( 'char' is CCS for that).
I've used 'char' for numbers 0-255 for decades......
to my eyes mday IS a 'char' or 8 bit unsigned but you're trying to print is as a string ?
Jay |
|
|
dluu13
Joined: 28 Sep 2018 Posts: 395 Location: Toronto, ON
|
|
Posted: Tue May 28, 2019 2:15 pm |
|
|
temtronic wrote: | OK, I'm old, tired, wet, and beatup.. |
Hey at least it's better than Saturday afternoon eh
As another comment, I always keep a visual studio or code blocks C project sitting around on my desktop that I can use to test code snippets in.
It's much faster than having to plug in the ICSP/bootloader of your PIC and much cleaner on the desk too! |
|
|
SeeCwriter
Joined: 18 Nov 2013 Posts: 160
|
|
Posted: Tue May 28, 2019 2:18 pm |
|
|
Changing %L to %Lu fixed the problem. It made no difference as to how "yr" was declared, whether signed or unsigned, or long or int16_t or uint16_t.
One thing that threw me off was that the compiler error was always on the third element, so that's where I focused.
Thanks for the help. |
|
|
dluu13
Joined: 28 Sep 2018 Posts: 395 Location: Toronto, ON
|
|
Posted: Tue May 28, 2019 2:20 pm |
|
|
I just made that comment on the variable type as a matter of coding practice. It all works, but different platforms have different size long/int so to make it explicitly clear, I prefer to say what exact size my variables are. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19538
|
|
Posted: Wed May 29, 2019 12:10 am |
|
|
diuu13 is 'dead right'.
Using 'long', 'short' etc., is asking for problems 'long term'.
These names have different meanings with different compilers. Even CCS
changes the meanings when you go to PCD. Using these, and you will
suddenly find the actual sizes are not what you expect.
Very, very, very much better to use 'explicit' sizes, that actually say what
the variable really is going to be. Ideally load stdint.h, and use the types
defined in this. Makes it at least 50* easier to port code using this in the
future to different processors, but equally importantly makes your
programming 'style' portable to other compilers/processors.
'long' says nothing at all about the real size of the variable. uint16_t, says
this is an unsigned 16bit integer. Much better. |
|
|
|