Home > Back-end >  Variable in C language when allocate space?
Variable in C language when allocate space?

Time:11-09

The compiler for GCC
The phenomenon of one:
The three code output how explanation?
 
#include
#include
Int main ()
{
Int b;
Int a;
Printf (" % x \ n ", & amp; A);//only output a address, I on the machine for 61 fe1c
system("pause");
return 0;
}

 
#include
#include
Int main ()
{
Int b;
Int a;
Printf (" % x \ n ", & amp; B);//output b address, only my machine also fe1c for 61
system("pause");
return 0;
}

 
#include
#include
Int main ()
{
Int b;
Int a;
Printf (" % % x x \ t \ n ", & amp; A, & amp; B);//output at the same time a and b address, my machine is 61 fe18 61 fe1c
system("pause");
return 0;
}

Why the front two output will address the same?
The phenomenon of two:
The two how to explain the code?
 
#include
#include
Int main ()
{
int b;
int a;
Printf (" a=% d \ n ", a);//output a=16
Printf (" \ n b=% d ", b);//output b=0
Printf (" % X \ n ", & amp; A);//a address is 61 fe18
system("pause");
return 0;
}

 
#include
#include
Int main ()
{
int b;
int a;
Printf (" a=% d \ n ", a);//output a=0
Printf (" \ n b=% d ", b);//output b=16
Printf (" % X \ n ", & amp; B);//address for 61 b fe18
system("pause");
return 0;
}

From a and b can be seen the value of the exchange of a, b address exchanged? Why a, b will address exchange?
The phenomenon of three:
How do you explain these two code results
 
#include
#include
Int main ()
{
Int b=10000;
Int a;
Printf (" % X \ n ", & amp; B);//address is 61 b fe1c
system("pause");
return 0;
}

 
#include
#include
Int main ()
{
Int b=10000;
Int a;
Printf (" a=% d \ n ", a);
Printf (" % X \ n ", & amp; B);//address is 61 b fe18
system("pause");
return 0;
}

Why this two b address changed?

CodePudding user response:

Int x this is stated but no initialization so no assigned address

CodePudding user response:

Assigned address program is run, you run twice, each time you run a same address don't be surprised, at the same time the output is not the same

CodePudding user response:

First of all, each time you run the program, the address is not necessarily the same, if the interval is very short run twice, are assigned in the same address, not surprisingly,
And you two pieces of code defines a and b, but only one is used, the compiler may have another variable void, so whether you look at which the address of the variable, because it is used, also showed the same address,
In addition, cannot use uninitialized variable value to what to do, because it is not affirmatory,

CodePudding user response:

refer to the third floor of the devil knows how to reply:
first of all, every time to run the program, the address is not necessarily the same, if the interval is very short run twice, are assigned in the same address, not surprisingly,
And you two pieces of code defines a and b, but only one is used, the compiler may have another variable void, so whether you look at which the address of the variable, because it is used, also showed the same address,
In addition, cannot use uninitialized variable value to what to do, because it is not affirmatory,

You give his address assignment and exchange of,

CodePudding user response:

reference 4 floor funnywhere response:
you give his address assignment and exchange of,


Same program launch process is allocated memory address is not necessarily the same, you here because of separated by short, may be reused the same memory

The other, [] to his assignment here mean? You don't have to two variables in the code assignment, do you mean the following two code?

 # include & lt; Stdio. H> 
#include
Int main ()
{
Int b=10000;//there are assignment
Int a;//there is no use a, compiling optimization may be directly abandon a
Printf (" % X \ n ", & amp; B);//address is 61 b fe1c//here is equivalent to only one variable, so just print a variable address
system("pause");
return 0;
}

#include
#include
Int main ()
{
Int b=10000;
Int a;
Printf (" a=% d \ n ", a);//is used here to a, the compiler can't take a give up, so the address of a 61 fe1c
Printf (" % X \ n ", & amp; B); Address is 61 fe18////b so here is the address of the 61 b fe18 one variable is the address of another (4 bytes)
system("pause");
return 0;
}


  • Related