Thread: int i=j ,why this not possible in global?

  1. #1
    kotin
    Join Date
    Oct 2009
    Posts
    132

    int i=j ,why this not possible in global?

    Code:
    int i=10;
    int j=i;
    int main()
    {
    printf("%d \n",j);
    return 0;
    }
    OUTPUT : error: initializer element is not constant

    Please anyone give explanation on above error.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You are initializing the variable with another variable.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Quote Originally Posted by quzah
    You are initializing the variable with another variable.
    Is this only not allowed for global variables? If "i" was "const", would this then be allowed (as global), since the value of "i" is known at compile-time? N.B. Please dont say "try it" as thats obvious, Id rather understand it.

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    82
    You may do like so in global,
    Code:
    #define evil 666
    const unsigned int lolwat = evil;
    The problem has to do with the instantiation of a 'variable' type, say it out loud and you'll see why. ;D

    \/
    Last edited by since; 12-09-2009 at 02:27 PM.

  5. #5
    kotin
    Join Date
    Oct 2009
    Posts
    132
    hi ,

    till now i am not clear my confusion about this problam.

    Actually wt will hapen in load time? y variable value is not assigning.

    wts internal machanisam going on in compile and load time?

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Global variables may only take constant initializers. It's obvious that the compiler could in PRINCIPAL figure out what to do, but it's not in the standard. It needs to be a constant.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  3. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM
  4. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM