Home > OS >  If we assign a new value to a variable, what happens to the old value ? (in C)
If we assign a new value to a variable, what happens to the old value ? (in C)

Time:12-31

From the book : "Programming in C (4th Edition)" by Stephen G Kochan

// Basic conversions in C

#include <stdio.h>

int main (void)
{
      float  f1 = 123.125, f2;
      int    i1, i2 = -150;
      char      c = 'a';

      i1 = f1;                 // floating to integer conversion
      printf ("%f assigned to an int produces %i\n", f1, i1);

      f1 = i2;                 // integer to floating conversion
      printf ("%i assigned to a float produces %f\n", i2, f1);

      f1 = i2 / 100;           // integer divided by integer
      printf ("%i divided by 100 produces %f\n", i2, f1);

      f2 = i2 / 100.0;           // integer divided by a float
      printf ("%i divided by 100.0 produces %f\n", i2, f2);

      f2 = (float) i2 / 100;     // type cast operator
      printf ("(float) %i divided by 100 produces %f\n", i2, f2);
      return 0;
}

I am new to programming and I was trying to learn how to program in C from the above mentioned book. And I came across this snippet of code for converting integers to floats and vice versa. I am a bit confused as to what actually happens.

Here is what I understand so far : The first few lines (float f1 = 123.135, f2 ; and the next two lines) declare the variables.

Then the line i1 = f1 ; assigns the value of 123.125 to the variable named i1 but since i1 is an integer, its truncated to 123.

Now, here is my question, in the next line f1 = i2 ; the value -150 is assigned to the variable named f1 but since its a float, the value is stored as a float i.e. -150.000000 but what happened to 123.125 ?

Does that value still exist somewhere in the computers memory ? if so how can I access it ? like before when that value was assigned to f1, I could just refer to it as f1 but now that it is no longer assigned to any variable, can I refer to it in some way ?

Like a more generalized version would be, if we assign a new value to a variable, what happens to the old value ?

I have taken a gander at In python when we assign a new value to a variable what happens to the old one? but does the same happen in C ?

CodePudding user response:

In C, the variable is a name that is used to make a piece of memory easier to use. When you assign a value to the variable, the value is stored into a piece of memory that corresponds to the variable name.

This means that upon storing a new value into the variable the old value is overwritten as the variable still references the same piece of memory. Generally, the first value isn't retained in any way that can later be accessed.

Now there are "exotic" ways of doing things that might not make this statement 100% true. There are Copy-on-Write systems (some of which are implemented in C) that do retain old values (in the general sense) when new values are written. However, these kinds of systems are not even present in the majority of C programs, and if you were using one, you'd know it.

CodePudding user response:

Does that value still exist somewhere in the computers memory ?

The general answer is: No, once you assign a new value to a variable the old value is gone and consequently you can't access it.

That said...

Depending on your compiler, initializers (like yours 123.125) may be part of the program memory and therefore the value may still be somewhere in memory. However, that doesn't really matter cause you can't know where and therefore can't access it. So for any practical use, it's gone...

CodePudding user response:

whenever you make a variable and assign it a value it gets stored in the memory but when we change the variable's value we are basically replacing it with new value.so the old value gets removed from the memory

  • Related