suggest parentheses around assignment used as truth value

Posted by Robert Powell on Wed 13 Jul 2005 10:58 AM — 5 posts, 25,718 views.

Australia #0
Ok, i have a funny think happening and im not entirly sure why, hopeing someone will be abloe to help me on this.

Im adding things into time_update so i can have certain things happen on different days, i added in the following code and nothing happened, the days came and went and no call to my function.

if(time_info.day == 24)
    {
      do_mystuff();      
    }

To get it to work i changed it to look as below, now when it is the 24th do_mystuff is called and all works as it should, which brings me to the warning im getting on compile.

if(time_info.day = 24)
    {
      do_mystuff();      
    }

update.c:3131:warning:suggest parentheses around assignment used as truth value. Why isnt the first example with the == working as it should, or better yet, why is the single = working when its not the right way to compare values, or am i missing something totaly here.

Thanks in advance.
USA #1
With your first example, are you sure the clock reaches 24 and doesn't cycle at 23 instead? The midnight hour is usually 0.

Your second example is warning you that you're assigning the value 24 to something the compiler thinks you're using for a comparison - which is what you've done. Now instead of checking to see if it's 24, it will always be 24 which is why it works.

Go back to the first example and see if checking for == 23 will do what you want.
Australia #2
Thsnks for making me think about whats goin on a bit deeper, tho there are 24 hours in a day time_info.hour, 30 days to the month time_info.days and 12 months to a year time_info.year

  case 24:
      time_info.hour = 0;
      time_info.day++;
      break;
    }
    
  if(time_info.day >= 30)
    {
      time_info.day = 0;
      time_info.month++;
    }

  if(time_info.month >= 12)
    {
      time_info.month = 0;
      time_info.year++;
    } 


I now think i see why it did not execute, in update_time it sits inside the switch untill one of the conditions are met to break out, Of couse its only going to break out on the beginning of a new day, when time_info.hour hits 24.

I will try again and this time i will let it cycle through a full day, and see what happens.
Amended on Wed 13 Jul 2005 09:47 PM by Robert Powell
USA #3
I think you have the right idea but some of your vocabulary is wrong; the code doesn't 'sit around in the switch until a break'. Rather, it enters the switch, takes the appropriate branch, and leaves that branch when it hits a break.

Since the switch opens with ++time_info.hour, and case 24 sets time_info.hour back to 0, the hour never really does reach 24. Also note that those if statements are outside the switch statement.
#4
First, make sure that your if statement isn't in a place where it would not execute because the function has already returned.

Otherwise, add a log statement at the first line within the if statement. That will tell you for sure if it is executing. If it doesn't execute, then I would submit a bug report to your compiler's vendor.