Why is it not possible to assign a global variable to anotherglobal variable..?

J

Jase Schick

say ..

I have a peice of code like,

i = 10 ;
z = i ; <---------all are global scope..

main()
----
---

but not allowing me to compile and get a.out
...

Then i did,

int i , z ;

i = z = 20 ; multiple varibles initialization in global scope..
main ()
------
------ which is also not working..

but multiple initializations in local scope...is working...

like,

main()
{
int i, z;
i = z = 20;
...... which is Ok when i print..

or int i, z;
i = 20 ;
z = i;
is also Ok...

Why in the Global variable case, the above is not working....
 
I

Ian Collins

say ..

I have a peice of code like,

i = 10 ;
z = i ;<---------all are global scope..

You can't do that. The only thing you can do with a variable outside of
a function is declare and initialise it to a compile time constant.

int i = 10;
int z = 10;

int main() I hope!
but multiple initializations in local scope...is working...

That's how the language works.
 
G

Gene

say ..

I have a peice of code like,

i = 10 ;
z = i ;  <---------all are global scope..


but not allowing me to compile and get a.out

Because it's an error. Someone will probably quote the Standard
provision that applies. The practical reason is that the values of
globals are designed to be established by a program loader without the
evaluation of any code. In most environments, the loader will be
incapable of determining the value of i.
..

Then i did,  

int i , z ;

i = z = 20 ; multiple varibles initialization in global scope..

This is also an error. Executable code must be inside a function
body.
but multiple initializations in local scope...is working...

like,

main()
{
int i, z;
i = z = 20;

This assignment statement would also work fine if i an z were global.
.....         which is Ok when i print..

or int i, z;
i = 20 ;
z = i;

This would also work fine if i and z are declared global but the
executable code is in main() or any other function.
 
K

Keith Thompson

Jase Schick said:
say ..

I have a peice of code like,

i = 10 ;
z = i ; <---------all are global scope..

main()
[snip]

Statements are legal only within function bodies.
 
S

Shao Miller

say ..

I have a peice of code like,

i = 10 ;
z = i ;  <---------all are global scope..

main()
----
---

but not allowing me to compile and get a.out
..

Then i did,  

int i , z ;

i = z = 20 ; multiple varibles initialization in global scope..
main ()
------
------      which is also not working..

but multiple initializations in local scope...is working...

like,

main()
{
int i, z;
i = z = 20;
.....         which is Ok when i print..

or int i, z;
i = 20 ;
z = i;
              is also Ok...

Why in the Global variable case, the above is not working....

Initialization and assignment are different things.

int i = 5, j = 6;

is a declaration of 'i' and 'j' as well as an initialization of 'i'
and 'j' with values.

int i, j;
i = 5;
j = 6;

declares 'i' and 'j', then in separate statements assigns (not
initializes) values to those objects.

Try to think about _when_ "global variables" are initialized. In:

int i = 5; /* 5 is constant, so fine to initialize outside of a
function */
int j = i; /* We need to know the value of i, but the value of i
_when_? */

Hope this helps.
 
N

Nick Keighley

Subject: "Why is it not possible to assign a global variable to
another global variable..?"

it is

say ..

I have a peice of code like,

i = 10 ;
z = i ;  <---------all are global scope..

main()

please don't post program fragments. This program works and assigns a
global to a global

#include <stdio.h>

int i;
int z;

int main (void)
{
i = 10;
z = i;
printf ("i is %d and z is %d\n", i, z);
return 0;
}
 
G

Geoff

"Nick Keighley" <[email protected]> ha scritto nel messaggio
#include <stdio.h>

int i;
int z;

int main (void)
{
i = 10;
z = i;
printf ("i is %d and z is %d\n", i, z);
return 0;
}
------------
#include <stdio.h>

int i;
int z;

int main (void)
{
i = i;
z = i;
printf ("i is %d and z is %d\n", i, z);
return 0;
}

i = i; ?????

If global variables are initialized to 0 by the compiler or by the
system then you might get zero, but Nicks original example used
i = 10;
 
K

Keith Thompson

io_x said:
------------
#include <stdio.h>

int i;
int z;

int main (void)
{
i = i;
z = i;
printf ("i is %d and z is %d\n", i, z);
return 0;
}

Nick's code demonstrated the issue (assign the value of a global
variable to another global variable) very clearly. Your version
changes "i = 10;" to the useless "i = i;" and depends on the fact
that objects with static storage duration are implicitly initialized
to 0.

Just what point were you trying to make?
 
B

Barry Schwarz

i = i; ?????

If global variables are initialized to 0 by the compiler or by the

Global variables have static storage duration. All variables with
static duration that are not explicitly initialized are initialized to
the appropriate form of 0 prior to main beginning execution.
 
B

Barry Schwarz

#include <stdio.h>

int i;
int z;
int square(volatile int value) {return value*value;}


int main (void)
{int i = i;

Since the value of the non-global i is indeterminate, what do you
expect to happen?
int z = i;

printf("i is %d and z is %d\n", i, z);
i=128;
z=square(i);
printf("square(%d)=%d\n", i, z);
return 0;
}


i is 2147323904 and z is 2147323904
square(128)=16384

"i" is only local in main()
and not global

There are two objects named i in your program but only one is ever
used, and incorrectly at that.
 
G

Geoff

yes i did read "int i=i;" even if it in text was only "i=i;"
for this
#include <stdio.h>

int i;
int z;
int square(volatile int value) {return value*value;}


int main (void)
{ int i = i;
int z = i;

printf("i is %d and z is %d\n", i, z);
/* above is programmer error */
i=128;
z=square(i);
printf("square(%d)=%d\n", i, z);
return 0;
}

The local variables i and z are not properly initialized before you
use them, this is why you get unexpected values.
The global i is not accessible in main() because the local i is in
scope.
i is 2147323904 and z is 2147323904
square(128)=16384

"i" is only local in main()
and not global

Yes. Because that's the way it works in C.
If you want to access global variables their names must be unique
within your program.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top