-1

Here is a MWE of something I came across in some C++ code.

int a = (int)(b/c);

Why is (int) after the assignment operator?

Is it not recommended to use it like this?

John3136
  • 28,809
  • 4
  • 51
  • 69
kapple
  • 1
  • 3
  • 1
    Depends on specific language (so, *update the question with appropriate/relevant tags*): presumably the result of `b/c` is a "float" and needs an explicit cast to an "int" before assignment. Not quite sure why "int" is considered a class either.. – user2864740 Nov 23 '18 at 00:35
  • There's no assignment operator in this code. In a definition the `=` symbol is not an operator; it's syntax which indicates that an initializer for the variable being defined follows. – M.M Nov 23 '18 at 04:05

4 Answers4

3

This is simply a C-style typecast. It is used to make the author's intentions explicit, especially when the result of b/c is of another type (such as unsigned or float).

Without the cast, you will often get a compiler warning about an implicit conversion which can sometimes have consequences. By using the explicit cast, you are stating that you accept this conversion is fine within whatever other limits your program enforces, and the compiler will perform the conversion without emitting a warning.

In C++, we use static_cast<int>(b/c) to make the cast even more explicit and intentional.

paddy
  • 60,864
  • 6
  • 61
  • 103
0

This is a cast used to convert a variable or expression to a given type. In this case if b and c were floating point numbers, adding the cast (int) forces the result to an integer.

Specifically this is a "C style cast", modern C++ has some additional casts to given even more control (static_cast, dynamic_cast, const_cast etc)

John3136
  • 28,809
  • 4
  • 51
  • 69
  • Does it return the integer part, round down, or round up? Also, thanks for responding, to all that identified this as a style cast. – kapple Nov 27 '18 at 00:21
  • Search is your friend: "does C cast round" will lead you here: https://stackoverflow.com/a/11128755/857132 – John3136 Nov 27 '18 at 00:25
0

It is not "(int) after the assignment operator".

It is "(int) before a float - the result of b/c".

It casts the float to an int.

Zhang
  • 3,030
  • 2
  • 14
  • 31
0

This is a mistake. In the code:

int a = b/c;

then it may cause undefined behaviour if the result of the division is a floating point value that is out of range of int (e.g. it exceeds INT_MAX after truncation). Compilers may warn about this if you use warning flags.


Changing the code to int a = (int)(b/c); has no effect on the behaviour of the code, but it may cause the compiler to suppress the warning (compilers sometimes treat a cast as the programmer expressing the intent that they do not want to see the warning).

So now you just have silent undefined behaviour, unless the previous code is designed in such a way that the division result can never be out of range.

A better solution to the problem would be:

long a = std::lrint(b/c);

If the quotient is out of range then this will store an unspecified value in a and you can detect the error using floating point error handling. Reference for std::lrint

M.M
  • 138,810
  • 21
  • 208
  • 365
  • 1
    I understand what you're getting at, but I don't think starting your message with a "This is a mistake" is a good idea. It gets people's attention but both the OP's line and the line that you gave does compile. It also might be fine for someone in their first hour or two of C (and then the lecturer/teacher might show why it's wrong in the third hour and use it to explain casting). – Ray Nov 23 '18 at 04:25
  • 1
    Anyway, to the OP, I think you should take such advice carefully. If you are taking a class and start using std::lrint, your teacher might wonder who else are you asking for help. It's ok to be a "little" wrong when you're starting out; learning is a step-by-step process. But yes, if you want to hear the ending to the story, that's ok too! – Ray Nov 23 '18 at 04:27
  • @Ray the Q/A on this site are for anyone to read, not just OP – M.M Nov 23 '18 at 06:14
  • 1
    I know. But your answer is given within the context of the OP's original question. Surely there's some compromise between providing information for anyone to read and sticking to the original OP's question. – Ray Nov 23 '18 at 15:14
  • I see what you're getting at @M.M, thanks for your response. FYI I'm not taking a class, I've learnt a few concepts here and there in C++, and am just trying to understand some code someone else wrote. – kapple Nov 27 '18 at 00:21