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

Discussion in 'C Programming' started by Jase Schick, Jul 31, 2010.

  1. Jase Schick

    Jase Schick Guest

    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....
    Jase Schick, Jul 31, 2010
    #1
    1. Advertising

  2. Jase Schick

    Ian Collins Guest

    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
    Ian Collins, Jul 31, 2010
    #2
    1. Advertising

  3. Jase Schick

    Gene Guest

    On Jul 31, 5:01 pm, Jase Schick <> 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.
    Gene, Jul 31, 2010
    #3
  4. Re: Why is it not possible to assign a global variable to another global variable..?

    Jase Schick <> 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.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jul 31, 2010
    #4
  5. Jase Schick

    Shao Miller Guest

    On Jul 31, 5:01 pm, Jase Schick <> 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.
    Shao Miller, Jul 31, 2010
    #5
  6. Subject: "Why is it not possible to assign a global variable to
    another global variable..?"

    it is

    On 31 July, 22:01, Jase Schick <> 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;
    }
    Nick Keighley, Aug 2, 2010
    #6
  7. Jase Schick

    Geoff Guest

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

    On Tue, 3 Aug 2010 20:04:19 +0200, "io_x" <> wrote:

    >
    >"Nick Keighley" <> ha scritto nel messaggio
    >news:...
    >#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;
    Geoff, Aug 3, 2010
    #7
  8. Re: Why is it not possible to assign a global variable to another global variable..?

    "io_x" <> writes:
    > "Nick Keighley" <> ha scritto nel messaggio
    > news:...
    > > #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) <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"
    Keith Thompson, Aug 3, 2010
    #8
  9. Re: Why is it not possible to assign a global variable to another global variable..?

    On Tue, 03 Aug 2010 11:22:24 -0700, Geoff <>
    wrote:

    >On Tue, 3 Aug 2010 20:04:19 +0200, "io_x" <> wrote:
    >
    >>
    >>"Nick Keighley" <> ha scritto nel messaggio
    >>news:...
    >>#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
    Barry Schwarz, Aug 4, 2010
    #9
  10. On 4 Aug, 06:09, "io_x" <> wrote:
    > "Keith Thompson" <> ha scritto nel messaggionews:...
    > > "io_x" <> writes:
    > >> "Nick Keighley" <> ha scritto nel messaggio
    > >>news:....


    > >> > #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
    Nick Keighley, Aug 4, 2010
    #10
  11. Re: Why is it not possible to assign a global variable to another global variable..?

    On Wed, 4 Aug 2010 13:06:10 +0200, "io_x" <> wrote:

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

    --
    Remove del for email
    Barry Schwarz, Aug 5, 2010
    #11
  12. Jase Schick

    Geoff Guest

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

    On Wed, 4 Aug 2010 13:06:10 +0200, "io_x" <> wrote:

    >
    >"Nick Keighley" <> ha scritto nel messaggio
    >news:...
    >>On 4 Aug, 06:09, "io_x" <> wrote:
    >>> "Keith Thompson" <> ha scritto nel
    >>> messaggionews:...
    >>> > "io_x" <> writes:
    >>> >> "Nick Keighley" <> ha scritto nel
    >>> >> messaggio
    >>> >>news:...

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

    >
    >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.
    Geoff, Aug 5, 2010
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. baumann@pan
    Replies:
    8
    Views:
    743
    Achintya
    May 10, 2005
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    851
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,733
    Smokey Grindel
    Dec 2, 2006
  4. datactrl
    Replies:
    3
    Views:
    124
    Thomas 'PointedEars' Lahn
    May 29, 2004
  5. Replies:
    5
    Views:
    102
    Peter Michaux
    Oct 5, 2007
Loading...

Share This Page