Thread: Global variable question

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    29

    Global variable question

    Consider this:
    Code:
    #include <stdio.h>
    
    int a = 10;
    
    int main(void)
    {
        int a = 15;
        int b = a;
    
        return 0;
    }
    What if I want to assign the value of global variable a to b?

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Then don't do something stupid like having two variables of the same name.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Make a pointer to it first.


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

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    In C++ you can reference it by saying:

    Code:
    int b = ::a;
    But that's entirely irrelevent here...

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    Quote Originally Posted by csisz3r
    Consider this:
    Code:
    #include <stdio.h>
    
    int a = 10;
    
    int main(void)
    {
        int a = 15;
        int b = a;
    
        return 0;
    }
    What if I want to assign the value of global variable a to b?
    If I recall correctly, the local variable will be used in this situation.
    Thus making your global variable usage in main useless.

    I know you were just curious. But avoid doing that.

  6. #6
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by csisz3r
    Consider this:
    Code:
    #include <stdio.h>
    
    int a = 10;
    
    int main(void)
    {
        int a = 15;
        int b = a;
    
        return 0;
    }
    What if I want to assign the value of global variable a to b?
    Easy, I'll make the change below:
    Code:
    #include <stdio.h>
    
    int a = 10;
    
    int main(void)
    {
        int a = 15;
        int b = a;
        b = 10;
        return 0;
    }

  7. #7
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    Quote Originally Posted by cwr
    Easy, I'll make the change below:
    Code:
    #include <stdio.h>
    
    int a = 10;
    
    int main(void)
    {
        int a = 15;
        int b = a;
        b = 10;
        return 0;
    }

    lol. guys, guys...

  8. #8
    ima n00b, ok? orion-'s Avatar
    Join Date
    Aug 2005
    Location
    alberta, canada
    Posts
    55
    pointers are your friend.

  9. #9
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    that's one great big reason why global variables are considered taboo -- its too easy to hide them, thus using the wrong version.

  10. #10
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    Code:
    #include <stdio.h>
    
    int a = 10;
    
    int main(void)
    {
       { int a = 15;}
        int b = a;
       
        return 0;
    }
    I think this will do what you want.i just remembered what my teacher said "FOOLISH QUESTION GET FOOLISH ANSWER".

    No offence

  11. #11
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Hey, my foolish answer is better

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  2. Static Local Variable vs. Global Variable
    By arpsmack in forum C Programming
    Replies: 7
    Last Post: 08-21-2008, 03:35 AM
  3. Static global variable acting as global variable?
    By Visu in forum C Programming
    Replies: 2
    Last Post: 07-20-2004, 08:46 AM
  4. global variable turns zero
    By Roaring_Tiger in forum C Programming
    Replies: 5
    Last Post: 04-07-2003, 12:52 PM