Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Why is it not possible to assign a global variable to anotherglobal variable..?

Reply
Thread Tools

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

 
 
Jase Schick
Guest
Posts: n/a
 
      07-31-2010
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....
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      07-31-2010
On 08/ 1/10 09:01 AM, Jase Schick wrote:
> 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;

> main()


int main() I hope!

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


That's how the language works.

--
Ian Collins
 
Reply With Quote
 
 
 
 
Gene
Guest
Posts: n/a
 
      07-31-2010
On Jul 31, 5:01*pm, Jase Schick <(E-Mail Removed)> wrote:
> 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.

> main ()
> ------
> ------ * * *which is also not working..
>


> 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.

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      07-31-2010
Jase Schick <(E-Mail Removed)> writes:
> 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

[snip]

Statements are legal only within function bodies.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Shao Miller
Guest
Posts: n/a
 
      07-31-2010
On Jul 31, 5:01*pm, Jase Schick <(E-Mail Removed)> wrote:
> 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.
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      08-02-2010
Subject: "Why is it not possible to assign a global variable to
another global variable..?"

it is

On 31 July, 22:01, Jase Schick <(E-Mail Removed)> wrote:
> 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


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;
}
 
Reply With Quote
 
Geoff
Guest
Posts: n/a
 
      08-03-2010
On Tue, 3 Aug 2010 20:04:19 +0200, "io_x" <(E-Mail Removed)> wrote:

>
>"Nick Keighley" <(E-Mail Removed)> ha scritto nel messaggio
>news:(E-Mail Removed)...
>#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;
>}
>----------
>result: "i is 0 and z is 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;
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      08-03-2010
"io_x" <(E-Mail Removed)> writes:
> "Nick Keighley" <(E-Mail Removed)> ha scritto nel messaggio
> news:(E-Mail Removed)...
> > #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;
> }
> ----------
> result: "i is 0 and z is 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?

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      08-04-2010
On Tue, 03 Aug 2010 11:22:24 -0700, Geoff <(E-Mail Removed)>
wrote:

>On Tue, 3 Aug 2010 20:04:19 +0200, "io_x" <(E-Mail Removed)> wrote:
>
>>
>>"Nick Keighley" <(E-Mail Removed)> ha scritto nel messaggio
>>news:(E-Mail Removed)...
>>#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;
>>}
>>----------
>>result: "i is 0 and z is 0"
>>
>>

>
>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.

>system then you might get zero, but Nicks original example used
> i = 10;


--
Remove del for email
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      08-04-2010
On 4 Aug, 06:09, "io_x" <(E-Mail Removed)> wrote:
> "Keith Thompson" <(E-Mail Removed)> ha scritto nel messaggionews:(E-Mail Removed)...
> > "io_x" <(E-Mail Removed)> writes:
> >> "Nick Keighley" <(E-Mail Removed)> ha scritto nel messaggio
> >>news:(E-Mail Removed)....


> >> > #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;
> >> }
> >> ----------
> >> result: "i is 0 and z is 0"


so what?


> > 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?

>
> "i=i" is a particular case here too,
> because in *"z = i;" the "i" is the local


there is no local i

> but in *"i = i;"
> the 2th "i" is the global one


they are the same i the program only contains one i


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
assign JavaScript variable to Java variable problem in JSP Matt Java 8 01-25-2010 04:48 PM
Can I assign an event to a global variable? TriFuFoos@gmail.com Javascript 5 10-05-2007 02:56 AM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
why can not assign value to an array variable of a structure? baumann@pan C++ 8 05-10-2005 10:01 AM
Is that possible I can assign an window object variable to an already opened window datactrl Javascript 3 05-29-2004 06:52 AM



Advertisments