Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

458
Vistas
When we typecast the int to double does the actual address where int is stored is get changed?

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?

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

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!

over 4 years ago · Santiago Trujillo Denunciar

0

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.

over 4 years ago · Santiago Trujillo Denunciar

0

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:

  1. in general, you must assume the address will be different
  2. in this case, the types don't even have the same size
  3. in my example, i is accessed later on, and
  4. in reality, that's not likely to be a good idea -- depending on what's about to happen to the double after casting on a modern CPU is that it will most likely be written into a special register dedicated to floating point processing, which will not be the one in which the int was.
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda