View previous topic :: View next topic |
Author |
Message |
Freddie
Joined: 06 Sep 2003 Posts: 49
|
How do you fprintf a #define ? |
Posted: Wed Sep 21, 2005 7:59 pm |
|
|
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
|
|
Posted: Wed Sep 21, 2005 8:58 pm |
|
|
Try this:
Code: |
int8 parameter;
parameter = _parameter1;
fprintf(serialPC,"Parameter1= %U", parameter); |
|
|
|
Ttelmah Guest
|
|
Posted: Thu Sep 22, 2005 2:15 am |
|
|
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 |
|
|
|