What happens if the result of an arithmetic operation is not stored in memory

When I studied C ++ 5 years ago, one of our assignments was

Create a program that calculates the temperature in degrees Fahrenheit based on the Celsius value using the formula C ° x 9/5 + 32 = F °

Our first version was something like this.

int main()
{
    float celsius;
    cout << "Enter Celsius temperature: ";
    cin >> celsius;
    cout << "Fahrenheit: " << celsius * (9.0 / 5) + 32 << endl;
    return 0;
}

Classmates noted that we were clearly not told to output the result, resulting in

int main()
{
    float celsius;
    cout << "Enter Celsius temperature: ";
    cin >> celsius;
    celsius * (9.0 / 5) + 32;
    return 0;
}

I used this as a joke: always have to be careful when specifying requirements

Recently, I was wondering if this code really meets the requirements.

celsius * (9.0 / 5) + 32; Dead Code? Visual Studio 2010 - .

Visual Studio, , - , , , float celsius; .

++

     7:     float celsius;
     8:     cout << "Enter Celsius temperature: ";
 push        offset string "Enter Celsius temperature: " (01368B30h)  
 mov         eax,dword ptr [_imp_?cout@std@@3V?$basic_ostream@DU?$char_traits@D@std@@@1@A (0136C098h)]  
 push        eax  
 call        std::operator<<<std::char_traits<char> > (01361370h)  
 add         esp,8  
     9:     cin >> celsius;
 mov         esi,esp  
 lea         eax,[celsius]  
 push        eax  
 mov         ecx,dword ptr [_imp_?cin@std@@3V?$basic_istream@DU?$char_traits@D@std@@@1@A (0136C09Ch)]  
 call        dword ptr [__imp_std::basic_istream<char,std::char_traits<char> >::operator>> (0136C0A0h)]  
 cmp         esi,esp  
 call        __RTC_CheckEsp (0136114Fh)  
    10:     celsius * (9.0 / 5) + 32;
    11:     return 0;
 xor         eax,eax  
+4
2

, , . , volatile float celsius;, !

+8

( ), . , .

.

+3

Source: https://habr.com/ru/post/1655217/


All Articles