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

Bad for loop - What happens?

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







Bad for loop - What happens?
PostPosted: Wed Jan 25, 2006 2:51 pm     Reply with quote

Hello All,

What happens when I have an integer variable and use it in a for loop that requires a long integer variable?

What would happen to the "t" variable in the code below? it will increment to 255 (since t is declared as integer) and then what? would it be reset to zero, if so, the loop will never exit right?

Code:

// Test
int t = 0;
fprintf(PC,"Begin Bad Counter...\r\n");
for (t = 1; t <= 1200; t++) delay_ms(50);
fprintf(PC,"End Bad Counter...\r\n");


Thank you in advance
Peter
coolio
Guest







PostPosted: Wed Jan 25, 2006 3:18 pm     Reply with quote

If you increment an unsigned 8-bit value past 255 you will end up at zero. For signed values you will hit -128 once you increment past 127.
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Thu Jan 26, 2006 8:37 am     Reply with quote

It will loop forever because t <= 1200 will always be true.
_________________
The search for better is endless. Instead simply find very good and get the job done.
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