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

How do you fprintf a #define ?

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



Joined: 06 Sep 2003
Posts: 49

View user's profile Send private message

How do you fprintf a #define ?
PostPosted: Wed Sep 21, 2005 7:59 pm     Reply with quote

I wrote some code that has many #defines, which will be changed by other programmers as the code is recompiled in the future.

For example:
Code:

#define _parameter1 200


Later in the code I have some fprintf statements:
Code:

fprintf(serialPC,"Parameter1= %U", _parameter1);



Now, if later the #define is changed to the following, I get an error with the fprinf statement because the "type" assinged to _parameter1 is not longer printable with %U.
Code:

#define _parameter1 200


I guess this is because the compiler automatically types the #define? I thought I could get around this by copying all the #defines to strings and them printing them but that takes up allot of ROM/RAM.

Anyone have and thoughts on how to do this a better way?

Thanks.
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Wed Sep 21, 2005 8:58 pm     Reply with quote

Try this:

Code:

int8 parameter;
parameter = _parameter1;
fprintf(serialPC,"Parameter1= %U", parameter);
Ttelmah
Guest







PostPosted: Thu Sep 22, 2005 2:15 am     Reply with quote

You can 'force type' a define.
So:

#define _parameter1 (int8)(200)

Will force the define to always be treated as an int8. Or:

#define _parameter1 (200L)

Will force the define to be a 'long' (you could also use an int16 cast the same way).

You should also get into the habit of bracketting defines. If a define contains arithmetic, and this is not done, the evaluation order may not be what is expected, and hence always bracketting is a good way of avoiding problems in the future. -)

Best Wishes
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