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

Not zero constant

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



Joined: 17 Sep 2004
Posts: 133
Location: UK

View user's profile Send private message

Not zero constant
PostPosted: Wed Nov 24, 2004 5:53 am     Reply with quote

Is there a way to define a not zero constant?
Something like:

#define CONST_NAME !0

Thank you
_________________
Alex
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Wed Nov 24, 2004 6:40 am     Reply with quote

Code:
#define CONST_NAME !0
#define CONST_NAME2 ~0

void main()
{
  int8 a, b;
  int16 c;
 
  a = CONST_NAME;      // a = 0x01
  b = CONST_NAME2;     // b = 0xFF
  c = CONST_NAME2;     // c = 0x00FF
}
alexz



Joined: 17 Sep 2004
Posts: 133
Location: UK

View user's profile Send private message

PostPosted: Wed Nov 24, 2004 6:47 am     Reply with quote

ckielstra wrote:
Code:
#define CONST_NAME !0
#define CONST_NAME2 ~0

void main()
{
  int8 a, b;
  int16 c;
 
  a = CONST_NAME;      // a = 0x01
  b = CONST_NAME2;     // b = 0xFF
  c = CONST_NAME2;     // c = 0x00FF
}


is the CONST_NAME always '1'?
what about all others except zero?
_________________
Alex
Mark



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

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

PostPosted: Wed Nov 24, 2004 7:15 am     Reply with quote

Quote:
Is there a way to define a not zero constant?


Maybe you should explain how you want to use it so that it will be more clear to us what you are trying to do.
Ttelmah
Guest







Re: Not zero constant
PostPosted: Wed Nov 24, 2004 10:49 am     Reply with quote

alexz wrote:
Is there a way to define a not zero constant?
Something like:

#define CONST_NAME !0

Thank you

Firstly, what you show here, is a macro, not a 'constant'. A macro, is just an instruction to the compiler to substitute the right hand text, where the 'name' is given. As such, it has some really major 'caveats'. If (for instance), you define a macro as shown, then code:

X = CONST_NAME*2;

This will be converted to:

X = !0*2;

Now the result will depend on the arithmetic precedence. '*', fortunately has a lower precedence than the 'not' operator, so the result will be as expected, but it is the cause of many unexpected results when using macro expansions like this. Hence it is always safer to 'bracket' the right hand term, which forces the entire 'assembly' to have a higher precedence.

If you want a 'non zero constant', the normal method, would be:

const int8 non_zero = !0;

Which will store a constant 8 bit integer, containing the logical 'not' of '0'.
That macros are not 'constants', can easily be demonstrated, by having a second #define, and changing the value.

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