Hello there, I would appreciate it if anyone can answer my question.
Identify the implicit cast and explicit cast?
int a = 2, b = 3;
float f = 2.5;
double d = -1.2;
int int_result;
float real_result;
Hello there, I would appreciate it if anyone can answer my question.
Identify the implicit cast and explicit cast?
int a = 2, b = 3;
float f = 2.5;
double d = -1.2;
int int_result;
float real_result;
well, this is actually a home work in C class, and there was an example answer given for the first line wirtten in the above, which i didnt undersand:which was,
int_result = a * f;
// a is casted implicitly to float by the multiplication operation a*f,
// the product is then casted implicitly to int by the = (assignment) operation.
real_result = a * f;
real_result = (float) a * b;
d = a + b / a * f;
d = f * b / a + a;
thanks again, look forward your replies!! :)
Formally, the question makes no sense from the terminological point of view. There's no such thing as "implicit cast". The whole point of the term cast is that it designates an explicitly-requested conversion. Cast is the type conversion explicitly requested by the operator of the (type)
form.
What in this case can be explicit or implicit is called conversion. This is what was probably meant by the author of the question, but screwed up by their poor knowledge of C terminology.
In your code sample only one initialization requires a conversion. And, of course, that conversion is implicit, since there are no casts in your code whatsoever.
int a = 2, b = 3;
float f = 2.5;
double d = -1.2; //This is an implicit cast.
int int_result;
float real_result;
There is no explicit cast in above statements.