Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of int(expr) in C++?

Tags:

c++

When looking over some code from a friend's project, I recently saw syntax that looked like this.

#include <iostream>

int main(){
    std::cout<< int(32.5/5)  << std::endl;
}

When you run the above code, you get 6, which is the expected value if the use of int functions like a cast.

However, I have never seen this syntax before and I could not find documentation for it on the web. I also did an experiment and noticed that this syntax is not valid in C.

Can someone explain the meaning of this syntax with documentation references?

like image 542
merlin2011 Avatar asked Jan 08 '23 21:01

merlin2011


1 Answers

This is not a constructor call or a "function". There is no "int function".

This is functional cast notation; it's just a cast.

It's the same as (int)(32.5/5) (in this particular case).

And, no, C does not have it.

like image 65
Lightness Races in Orbit Avatar answered Jan 14 '23 13:01

Lightness Races in Orbit