Home > Software design >  What purpose does the dot( . ) fulfil in this piece of code
What purpose does the dot( . ) fulfil in this piece of code

Time:10-17

In an example that's in my C book, I've found this piece of code at the end of an example problem, which verified if 3 integers were in an arithmetic progression.

if (b==(a c)/2.)

I don't think I've seen the dot after 2 ever used in such a way and I don't know what it's purpose is here.

CodePudding user response:

2. is a double literal. It's the same as 2.0.

Integer division is different than floating point division, so in some cases having a double instead of an int makes an important difference.


Although this form is perfectly valid, for readability purposes often 2.0 is preferred. In some (not all) newer languages derived from C like C# it is not allowed, i.e. you are forced to write 2.0.

  •  Tags:  
  • c
  • Related