CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Format error

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
SeeCwriter



Joined: 18 Nov 2013
Posts: 159

View user's profile Send private message

Format error
PostPosted: Tue May 28, 2019 12:47 pm     Reply with quote

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: 1903

View user's profile Send private message

PostPosted: Tue May 28, 2019 1:16 pm     Reply with quote

Instead of strcpy, try sprintf instead just to see if that makes a difference.
dluu13



Joined: 28 Sep 2018
Posts: 395
Location: Toronto, ON

View user's profile Send private message Visit poster's website

PostPosted: Tue May 28, 2019 1:19 pm     Reply with quote

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: 9134
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue May 28, 2019 2:10 pm     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Tue May 28, 2019 2:15 pm     Reply with quote

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: 159

View user's profile Send private message

PostPosted: Tue May 28, 2019 2:18 pm     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Tue May 28, 2019 2:20 pm     Reply with quote

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: 19259

View user's profile Send private message

PostPosted: Wed May 29, 2019 12:10 am     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group