Michael J. Harris
Michael J. Harris

Reputation: 33

Assignment inside an expression?

What is this code equivalent to?

int sec, mins, hours, days;
cin >> sec;
sec -=  (60 * 60 * 24) * (hours = sec / (60 * 60 * 24));
sec -=  (60 * 60) * (days = sec / (60 * 60));
sec -=  60 * (mins = sec / 60);

This code was written by my friend to calculate how many days, hours, minutes, seconds in an input entered in seconds. This seems so ambiguous to me.

sec -=  (60 * 60 * 24) * (hours = sec / (60 * 60 * 24));

Why does this line mean? I'm so confused by two assignment in a single expression. Is embedded assignment valid in standard c++? Regardless of the whole code.

Upvotes: 1

Views: 453

Answers (4)

Zebrafish
Zebrafish

Reputation: 13886

First of all, the hours and days variables are mixed up, days should go first. I think substituting all those numbers with one variable helps:

#include <iostream>

#define secondsInaDay (60 * 60 * 24)
#define secondsInAnHour (60 * 60)
#define secondsInAMinute 60

int main() 
{
    int totalSeconds, mins, hours, days;
    cin >> totalSeconds;

    totalSeconds -= (secondsInaDay) * (days = totalSeconds / (secondsInaDay));
    // First divides the total seconds by the seconds in a day, giving the number of days.
    // Then subtracts the seconds that have been accounted as days on the left side.

    totalSeconds -= (secondsInAnHour) * (hours = totalSeconds / (secondsInAnHour));
    // First divides the remaining seconds by seconds in an hour, giving the number of hours.
    // Then subtracts the seconds that have been accounted for as hours.

    totalSeconds -= secondsInAMinute * (mins = totalSeconds / secondsInAMinute);
    // First divides the remaining seconds by seconds in a minute to give the number of minutes
    // Then subtracts the seconds that have been accounted for as minutes.

    int seconds = totalSeconds;
    // Any remaining seconds are added to seconds

    return 0;
}

Upvotes: 0

ikh
ikh

Reputation: 10417

= operator receives two arguments, assigns the right one into left one, and returns the value assigned. Remeber, binary operator is just binary function.

Example:

#include <iostream>

int main()
{
    int i;
    std::cout << (i = 42);
}

Output:

42

(ideone)

Upvotes: 0

Weibin Kong
Weibin Kong

Reputation: 1

sec -= (60 * 60 * 24) * (hours = sec / (60 * 60 * 24));

equals:

   days = sec / (60 * 60 * 24);
   days_secs = (60 * 60 * 24) * days;
   sec -= days_secs;

the variable "hours" is not so pretty,it should be "days". Similarly, the variable "days" in

sec -=  (60 * 60) * (days = sec / (60 * 60));

should be "hours".

good luck.

Upvotes: 0

Mateen Ulhaq
Mateen Ulhaq

Reputation: 27191

The line

sec -= (60 * 60 * 24) * (hours = sec / (60 * 60 * 24));

is equivalent to

hours = sec / (60 * 60 * 24);
sec -= (60 * 60 * 24) * hours;

The construction (a = b) is considered an expression that returns the value b. Here's a couple of examples of equivalent expressions:

5
4 + 1
2 * 2 + 1
2 * 2 + (a = 1)

So it all works out! That said... don't write code like this.

Upvotes: 4

Related Questions