I am asking this question for two different languages: C and C++.
What is best practice when calling functions that have an opposite integer sign expectation to what we require in our code?
For example:
uint32 _depth; // uint32 = DWORD
int depth;
_BitScanForward(&_depth, (uint32)input); // DWORD, DWORD
depth = (int)_depth;
_BitScanForward is expecting DWORD (uint32) parameters. The variable input
is of int16 type and I need to process the result _depth
as an int32 in my code.
- Do I need to care about casting
input
as shown? I know the complier will probably do it for me, but what is best practice? - Is it acceptable to declare
_depth
as int32 and therefore avoid having to cast it afterwards as shown?
NOTE:
My comment about the complier is based on experience. I wrote code that compiled with no warnings in VS but crashed on execution. Turned out I was calling a function with an incorect width int. So I don't leave this topic up to the compiler any more.
EDIT:
The answers are helpful, thanks. Let me refine my question. If there are no width issues, i.e. the function is not expecting a narrower int than what is being passed in (obvioulsy will fail), then is it okay to rely on the compiler to handle sign and width differences?