[Solved] Explicit type casting operator in C/C++


i = int (f);

is valid in C++ but not in C.

From the C99 Standard, 6.5.4 Cast operators

 cast-expression:
      unary-expression
      ( type-name ) cast-expression

C++ supports the above form of casting as well as function style casting. Function style casting is like calling the constructor of a type to construct on object.

Check out the following block code compiled using C and C++.

#include <stdio.h>

int main()
{
   float f = 10.2;
   int i = int(f);

   printf("i: %d\n", i);
}

Non-working C program: http://ideone.com/FfNR5r

Working C++ program: http://ideone.com/bSP7sL

solved Explicit type casting operator in C/C++