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

C math question

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



Joined: 23 Feb 2009
Posts: 29

View user's profile Send private message

C math question
PostPosted: Tue Mar 03, 2009 3:27 pm     Reply with quote

How does CCS C store intermediate variables that are larger than the final storage type? Like this:

Code:

int16 var1;
int16 var2;

var1=(var2*99)/100;


Clearly the result will fit in var1, but does var1 need to be an int32 to store the intermediate result (var*99)? Or is the intermediate result stored on the stack as an int32? Do I need to cast anything?

Sorry for the newb question, I should know this but I cannot remember.
Sydney



Joined: 13 Feb 2009
Posts: 71

View user's profile Send private message

PostPosted: Tue Mar 03, 2009 3:42 pm     Reply with quote

Quote:
does var1 need to be an int32 to store the intermediate result (var*99)?


Yes.

Code:
var1=(int32)var2*99/100
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Tue Mar 03, 2009 4:30 pm     Reply with quote

This behaviour is required by the C standard and not specific to CCS C. An expression gets the type of the "largest" member.
bsturm



Joined: 23 Feb 2009
Posts: 29

View user's profile Send private message

PostPosted: Tue Mar 03, 2009 4:33 pm     Reply with quote

Thanks,

I suspected it was that way, but I didn't want to waste a couple of bytes of memory if I didn't have to.
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