Does [casting] have any implications on performance?
Not directly. A type cast expresses an explicit conversion of a value from one data type to another. It is really the conversion that has the potential for a performance impact, and if such a conversion is going to be performed whether you cast or not, then the cast has no performance impact whatever.
For example, some compilers can be set to warn about implicit conversions from floating-point types to integer types, and often these warnings can be silenced by making the conversion explicit with a cast. That does not change the fact that a conversion will be performed, and that such conversions are not free, but the cast doesn't make the conversion any more expensive than it otherwise would be.
Additionally, some conversions can be implemented for free. For example, on most machines, signed and unsigned integer types with of the same width have compatible representations, so converting values between these types is a no-op. Casts that only add or remove type qualifiers other than _Atomic
are also in this category.
With regard to your specific examples:
1) struct X *
pass to a function which takes void *
C does not require compatible representations for different pointer types, but in practice it is rare these days for different object pointer types to have different representations. Therefore, conversions between pointer types are usually free. That hardly matters, however, because the specific case you ask about is one in which the conversion will be performed whether you insert an explicit cast or not.
2) uint16_t
typecasted to uint32_t
This may be free, depending on specific circumstances and compiler implementation. For example, if the value being converted was already being held in a 32-bit register, then it is a no-op. Additionally, the compiler might be able to implement it as a no-op in the specific expression in which it appears. Note also that if uint32_t
is the same as unsigned int
, as is common, then C semantics require this particular conversion to be performed routinely in the evaluation of arithmetic expressions, so many of these fall into the category of conversions that will happen whether you cast or not.