User_defined() vs main(), the address of the float variable showing different, yet the same variable and value being used, why????

0 pratik Vyas · June 2, 2015
i wanted to know if the variable being used by the program is same in both places or being declared/initialized separately. And what I understand is that two variables having different addresses are different in such cases.

My Question: Shouldn't the programme be Not giving correct output? because main function has a different, float a, variable and the tables1() function has a different, float a, variable as we can even see the address is different for both in the output. Yet it gives out multiples as required.

Shouldn't the address of these variables be same, if the functions are using the same values from the same variables and giving the correct output?

My output and Code given below


    #include <stdio.h>
#include <stdlib.h>

float tables1(float a,float b)
int i=0; 
        printf(" %.2f\t %.2f \n", a*i,b*i); 
    printf("\n\nif all tasks not executed plz check the code \n"); 
    printf("the address of , float a, in tables1 function is %d \n\n", (int)&a); 
return 0; 


int main() 

    float a,b; 
    printf("enter the numbers whose first few multiples u want to see\n"); 
    scanf("%f", &a); 
    scanf("%f", &b); 
    printf("\nthe address of, float a, in the main function before \ncalling tables1 function is %d \n\n", (int)&a); 
    printf("following are the multiples \n \n "); 


    printf("the address of, float a, in the main function is %d \n\n", (int)&a); 
      return 0; 

Post a Reply


Oldest  Newest  Rating
0 c student · June 3, 2015
to understand what happens in your program, we first have to look down into memory.  try to keep up.
here is your program's code section loaded into memory when it is run:
function stacks:

we can see that your main stack is in a different position than your tables1 stack where it is below your tables1 function.  as you run your program, your main function is loaded first and then any other functions called are "stacked" onto it from higher memory to lower memory (the address values descend as you go up).  all of these values you see are just assembly instructions seen here:
disassembly of main:

disassembly of tables1:

watching the disassembly, as i step through the program, it will begin loading items onto a different stack where all your variables are stored.  the beginning of this stack is held in the stack pointer (called rsp in this case):

i took the liberty of fixing up your code so it prints the hexadecimal address instead of the decimal address (as you have done) to make things easier for us.  at the top, we can see that the scanned values are 2 and 3, the printf() tells us the address of a and as we examine the stack pointer, we can see our values 2 and 3.

as we step into the tables1 function, and with the guidance of the printed address of a, we can see these two values in a different address than main's as you have already concluded:
tables1 stack pointer:

you will also see that these two variables have swapped positions.  this happens because your variables are effectively copied and pushed onto the stack in reverse order (reverse to how you listed them in your function call) which you can observe in the disassembly for main where there are all of these mov instructions before the tables1 function is called.

so we can see that these two values are the same however, they are not the same in regards to their origin.  any parameters passed into a function is merely copied to that function and the original is kept the same despite changing them in your function.  if you wanted to pass the original values in then you need to pass it into the function by reference where when you change the values in your function, it will affect the originals directly.

remember, variables are there to make things easier for humans.  in the heart of a computer, variables do not exist.
  • 1



One of the most popular languages of all time.

Bucky Roberts Administrator