I cannot help myself but struggle to understand why is the ternary conditional's 3rd operand (the false-condition expression) subject to implicit type conversion even when the control condition evaluates to true and vice-versa?
I can probably only reason with the fact that the C language standard specification requires that the operands are "compatible" (I've heard this terminology everywhere, but I am not sure how accurate it is) and just like in any expression, the compiler makes sure that they are. But why is this restriction on the first place? The ternary is not like other operators in that it can freely disregard the expression that will not be evaluated. For other operations it is evident that the operands ought to be of the same type for the particular operation to work as intended.
That would also only make sense if the Implicit Type Conversion occur at compile time, and to be honest, I don't even really know when it takes place. For some reason I cannot find a sane answer to this question, but I did found this article. This article doesn't make any sense to me, particularly this line:
Implicit type conversion in C language is the conversion of one data type into another datatype by the compiler during the execution of the program.
Is it by the compiler, or is it during the execution of the program? I may be an easy one to get confused, but as far as I am aware the compiler has nothing to do during the program's execution.
So, can anyone please explain why is the ITC conversion all that necessary, because to me this yields an unnecessary overhead. Here is a proof-of-concept:
#include <stdio.h>
int main (void)
{
printf("%zu\n", sizeof( 0 ? 2.0 : 3 ));
return 0;
}
The result is 8
.
- Also I asked ChatGPT when does ITC occur and it wrote at run time. I asked it if it is sure and to validate that information and not surprisingly it changed its narrative.