I want to know that when we typecast the int to double does the actual result where int is stored is get changed or increased? because int is of 4 bytes(lets assume) and when we it get typecast to double which is of 8 bytes(assumption) then does the size also increased now to store the value of double? And please go easy on me if it is a stupid question?
Casting doesn't affect the variable and the corresponding memory. It's just and indication for the compiler how to interpret the bits the reside at the given location.
In Marcus'es example:
int i = 12;
double d = (double)i;
d is a new variable in a completely new location. The original value, i, is not affected. However, if you start playing with pointers then you have to be careful:
int i = 12;
double *p = &i;
Now, if double is 8 bytes wide then note that by using p you/compiler assume that it points to a memory location that has 8 bytes of memory allocated for the variable that it's pointing to. This is however not true, because in fact it's pointing to i, which is only 4 bytes wide (assume that int is 4 bytes wide).
EDIT
This is a (relatively) recent edit motivated by OPs request for clarification in the comments. The OP wants to know:
what happens to the memory address of sum when it get typecast does now typecasted value of sum stored somewhere else in the memory
and here's the code:
int sum = 17, count = 5;
double mean;
mean = (double) sum / count;
There's actually quite a lot going in the above three lines of code. However, the important thing here is that the variable sum is not being modified at all. It's only being used as an argument in addition. For the sake of adding two variables you only need the corresponding values. Before adding, the compiler will most likely copy the two variables into registers. Because you're casting to a double, the compiler will very likely store the value of sum in a 64 bit wide register (assume that double is 64 bit wide) and that's it. However, bear in mind that this is implementation specific and will vary from one compiler to another. It's not something that's specified by the C standard.
At this point, if you want to understand more you're best of compiling to Assembly and trying to understand that. Hope this helps!
That is the same question as "in a = b + c * d; where is the intermediate result of c * d stored?".
Simple answer: in a temporary variable manged by the compiler. This will most likely be a register. The language standard does not define this - why should it actually?
The typecast is actually similar to -b (b negated). This would also have to be stored somewhere prior to being used.
Note that the original value is not modified and the receiving variable has to be of the proper type anyway (that is one reason why you cannot typecast the receiving variable).
The int to double/float conversion is done by the compiler implicitly, so there is no need for an explicit cast - and it should not be used in such cases, as it will hide problem arising by later changes to the types.
I want to know that when we typecast the int to double does the actual result where int is stored is get changed or increased?
Since int and double in C are two distinct types, yes, casting an int to a double cannot happen in the same place where the int was -- even if these two types had the same size.
To illustrate: if casting overwrote the original variable, what would the effects of
int i = 12;
double d = (double)i;
int i2;
i2 = i + i;
be?
EDIT: @Dani added that
The double can occupy the same address if the int is no longer used.
Yes, compilers can check whether a variable will/could be accessed at a later point and can do operations in-place if that helps; however that's not the case here:
i is accessed later on, andint was.