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 CCS Technical Support

sprintf 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: 160

View user's profile Send private message

sprintf format error
PostPosted: Mon Jun 01, 2015 9:32 am     Reply with quote

When a variable is declared as an int, the following sprint() statement fails to compile with a format error:
Code:

int myvar = 3;
sprint( ptr, "%d", myvar );


If I change the format to %c, it compiles. Or if I change the variable declaration to byte, which is int8_t, it compiles.

Does this make sense?

v5.046 PCWHD, 18F87J60.
SeeCwriter



Joined: 18 Nov 2013
Posts: 160

View user's profile Send private message

PostPosted: Mon Jun 01, 2015 9:35 am     Reply with quote

"sprint" is a typo, it should be sprintf.
Ttelmah



Joined: 11 Mar 2010
Posts: 19549

View user's profile Send private message

PostPosted: Mon Jun 01, 2015 10:21 am     Reply with quote

Do you possibly have a #type declaration in your code?.

Remember a default 'int' is unsigned, while %d prints a signed variable. Normally though I'd not expect that to complain, but if int had been re-declared, so it was an int16 (for example), then the printf would complain.

Just tried it, and if I have:

#type int=16

then it complains exactly as you describe.

You then have to use %ld


Last edited by Ttelmah on Mon Jun 01, 2015 10:37 am; edited 1 time in total
SeeCwriter



Joined: 18 Nov 2013
Posts: 160

View user's profile Send private message

PostPosted: Mon Jun 01, 2015 10:32 am     Reply with quote

I have a number of typedef's, but I did not redefine 'int'.

I even tried format %u, and it still failed. It doesn't make sense.
Ttelmah



Joined: 11 Mar 2010
Posts: 19549

View user's profile Send private message

PostPosted: Mon Jun 01, 2015 10:39 am     Reply with quote

Not typedef's #type.

This changes the default size of a variable.

With int re-declared as an int16, then the %ld format has to be used.

Do a search in your code for #type. There will be one in the chip include file, so look carefully at what they all say.
SeeCwriter



Joined: 18 Nov 2013
Posts: 160

View user's profile Send private message

PostPosted: Mon Jun 01, 2015 11:37 am     Reply with quote

I've searched all the files in PICC\Devices and in PICC\Drivers and I could find no #type statements. I also searched all my project files, and no #type.

But I'm beginning to think there is something else going on. The compiler is acting differently. It no longer generates a list of compiler errors, it just lists one error at a time. Fix that error and recompile, and it lists one error...etc. And the errors I'm getting now don't make sense, such as the sprintf() format error. I tried %u, %d, %ld, %lu, and they all generate a format error if the variable is declared as 'int'.

So I change the variable to 'byte', and that error goes away, but the next one is "Function used but not defined." But it is defined. I have a whole file that is nothing but function prototypes that is included right after the chip header file. Nevertheless, I move the prototype into the same file as the call. Still get the error. Move the prototype to just before the call, still get the error. I finally rewrite the function into a #define statement. Now the error is gone, but a new one shows up, and it's the next line of code: Function used but not defined. Comment out that call, and the next line is flagged as an undefined error. This is all bogus. I think the project is corrupt or there is a problem with the new compiler version, v5.046.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jun 01, 2015 11:56 am     Reply with quote

In your previous thread, I point out that the CCS modified stack does
have #type statements in it, and 'int' is redefined as 16 bits:
http://www.ccsinfo.com/forum/viewtopic.php?t=53963&start=2&highlight=type
SeeCwriter



Joined: 18 Nov 2013
Posts: 160

View user's profile Send private message

PostPosted: Mon Jun 01, 2015 12:12 pm     Reply with quote

I did not remember that. However, I have wrapped all TCP/IP stack statements in #ifdef's and have removed TCP/IP from the project. I also renamed the tcpip directory created under my project directory just to make sure no stack files were being accessed.

That will present a problem in the future when I try to enable Ethernet capability and have to go through the program re-declaring many variables.
Ttelmah



Joined: 11 Mar 2010
Posts: 19549

View user's profile Send private message

PostPosted: Mon Jun 01, 2015 1:02 pm     Reply with quote

First, start with a much smaller (literally ten line) program, and verify that things work correctly.

Then look at #device=ANSI. This makes the default integer size int16, so %d will print an int16. Makes the variable sizes match what the ethernet stack will expect.

Then consider if you might have something really simple, like a #define that is affecting your int definition.

On your 'error' behaviour, there is a tick box in the project options 'show all errors'. If this is unticked, only the first error will be shown. From the command line, this is the -E option. It does suggest that some project options have been changed. Look at everything here carefully. Perhaps a global define?.
SeeCwriter



Joined: 18 Nov 2013
Posts: 160

View user's profile Send private message

PostPosted: Wed Jun 03, 2015 4:57 pm     Reply with quote

Even wrapping all the Ethernet includes and configurations in #ifdef's to remove them from the program did not solve the problem. My last resort was to start over.

I created a new project with no Ethernet capability, and copied over all my non-Ethernet related code to the new project. And now all the weirdness is gone, I can build an executable, and it's running on my hardware.
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