-3

The following piece of code outputs 0 and I'm not sure why. I'm not sure what the meaning of of . is in this context. Is it an operator or is it just indicating a float? Is it related to *Int?

#include <iostream>
using namespace std;
int main(){
   int *Int = new int;
   *Int = 1 / 2 * 2 / 1. * 2. / 4 * 4;
   cout << *Int;    
   return 0;
}
Some programmer dude
  • 380,411
  • 33
  • 383
  • 585
kpop02
  • 23
  • 3

2 Answers2

1

It's not an operator. It indicates a double, not a float.

42. means 42.0, and .42 means 0.42. A . alone is a compiler error (rather than 0.0).

If you add a trailing f, it will become a float instead of double, e.g. 1.f, .1f, 1.0f.

HolyBlackCat
  • 63,700
  • 7
  • 105
  • 170
1

1 / 2 == 0, 0 multiplied by anything is 0 again.