5
#include<stdio.h>
 int main()
 {
   int a;
   printf(" %u ",&a);
   return 0;
 }

The address we get is the virtual address of the process or the physical address when the process is running in main memory. Please help I am confused !!

user3111412
  • 177
  • 1
  • 3
  • 11
  • Who told you that addresses are "virtual" or "physical"? Certainly not C! – Kerrek SB Jan 27 '14 at 14:07
  • you can check these answers... and – ankit_c Jan 27 '14 at 14:10
  • What you get is something that when you `*` the result, you get back the same variable that you started with. If you are relying on anything other than that fact, you're relying on an implementation detail rather than a guarantee of the language. – Eric Lippert Jan 27 '14 at 16:24

2 Answers2

8

If you run the program on a system with virtual memory, you will get a virtual address. If you run on a system without virtual memory (typically smaller embedded systems), you will get a physical address.

Also note that the format "%u" is wrong for pointers, if you want to use printf to print a pointer you should use "%p". See e.g. this reference.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
4

Processes on systems with virtual memory always deal with virtual addresses. You wouldn't be able to use a physical address from within a process.

This is most easily verified by making the program run in a loop and print the value with some delay, then running multiple copies of the same program. Chances are they will print the same address (unless the OS is randomizing the virtual address usage), which of course would be impossible if the addresses were physical.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • Yes,like when you simply fork(). But if suppose one has to change the value of variable 'a' then how does OS do that ?? – user3111412 Jan 27 '14 at 14:08