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

217
Vistas
How values are changed in C unions?
#include <stdio.h>

int main()
{
 typedef union{
     int a ;
     char c;
     float f;
 } myu;
 
 myu sam;
 sam.a = 10;
 sam.f=(float)5.99;
  sam.c= 'H';

 printf("%d\n %c\n %f\n",sam.a,sam.c,sam.f);
 return 0;
}

Output

1086303816

H

5.990025

How come the value of integer has changed so drastically while the float is almost the same.

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

0

The fields of a union all share the same starting memory address. This means that writing to one member will overwrite the contents of another.

When you write one member and then read a different member, the representation of the written member (i.e. how it is laid out in memory) is reinterpreted as the representation of the read member. Integers and floating point types have very different representations, so it makes sense that reading a float as though it were an int can vary greatly.

Things become even more complicated if the two types are not the same size. If a smaller field is written and a larger field is read, the excess bytes might not have event been initialized.

In your example, you first write the value 10 to the int member. Then you write the value 5.99 to the float member. Assuming int and float are both 4 bytes in length, all of the bytes used by the int member are overwritten by the float member.

When you then change the char member, this only changes the first byte. Assuming a float is represented in little-endian IEEE754, this changes just the low-order byte of the mantissa, so only the digits furthest to the right are affected.

over 4 years ago · Santiago Trujillo Denunciar

0

Try this. Instead of using printf (which will mainly output nonesens) show the raw memory after each modification.

The code below assumes that int and float are 32 bit types and that your compiler does not add padding bytes in this union.

#include <string.h>
#include <stdio.h>
#include <assert.h>

void showmemory(void* myu)
{
  unsigned char memory[4];
  memcpy(memory, myu, 4);

  for (int i = 0; i < 4; i++)
  {
    printf("%02x ", memory[i]);
  }
  printf("\n");
}

int main()
{
  typedef union {
    int a;
    char c;
    float f;
  } myu;

  assert(sizeof(myu) == 4);  // assume size of the union is 4 bytes

  myu sam;
  sam.a = 10;
  showmemory(&sam);

  sam.f = (float)5.99;
  showmemory(&sam);
 
  sam.c = 'H';
  showmemory(&sam);
}

Possible output on a little endian system:

0a 00 00 00      // 0a is 10 in hexadecimal
14 ae bf 40      // 5.99 in float
48 ae bf 40      // 48 is 'H'
over 4 years ago · Santiago Trujillo Denunciar

0

How come the value of integer has changed so drastically while the float is almost the same.

That is just a coincidence. Your union will be stored in 4 bytes. When you assign the field "a" to 10, the binary representation of the union is 0x0000000A. And then, when you assign the field f to 5.99, it becomes 0x40bfae14. Finally, you set the c to 'H' (0x48 in Hex), it will overwrite the first byte, which corresponds to the mantissa of the float value. Thus, the float part changes slightly. For more information about floating-point encoding, you can check this handy website out.

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