Thread: Global space

  1. #1
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751

    Global space

    I was just wondering if i can expect anything bad from the following code, or whether it is just bad programming practice
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    const int RUNNING = 1;
    int correct = 0, wrong = 0;
    most notably the last line.


    also how do i declare these - the last line - in main and have all my functions access them? do i need to use extern or is there another way? and why does it give me an error msg when i do try to have a function call it from main()?

    thanks.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Just pass them as parameters to your functions. If the function will be changing the value, you'll need to pass a pointer to the variable.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    You actually want correct and wrong to have the same value? I take it you are not using these as some sort of switch.

    There is a way for your functions to have those values. Pass them as arguments.

    Could you post a little more code to see what you have going on?

  4. #4
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    no, no they are not constants. I'm using them to see how many answers a user has right and wrong. However I want to declare them once and then call them in ANY function called from main. is ther a way for this, aside maybe from extern.

    Thanks.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1) Use globals, as you've done:
    Code:
    #include stuff here
    
    int myglobal;
    
    void fun( void )
    {
        myglobal = 10;
    }
    
    void showglobal( void )
    {
        printf("myglobal is %d", myglobal );
    }
    
    int main( void )
    {
        myglobal = 4;
        showglobal();
    
        fun();
        showglobal();
    
        myglobal--;
        showglobal();
    
        return 0;
    }
    That's a poor way to do it. But some times, used sparingly, it works out ok.

    2) Pass the variable via function calls:
    Code:
    #include stuff here
    
    void somefunction( int x )
    {
        printf("x is %d\n", x );
    }
    
    void someotherfunction( int *x )
    {
        printf("x is %d, ", *x );
        *x = 234;
        printf("but now it's %d!\n", *x );
    
    }
    
    int yetanotherfunction( void )
    {
        printf("Changing your variable via return value...\n");
    
        return 1034;
    }
    
    int main( void )
    {
        int myvariable = 0;
    
        somefunction( myvaraible ); /* available, but won't be modified */
        printf("myvariable is %d\n", myvariable );
    
        someotherfunction( &myvariable ); /* available, can be modified */
        printf("myvariable is %d\n", myvariable );
    
        myvariable = yetanotherfunction( ); /* modified by the return value */
        printf("myvariable is %d\n", myvariable );
        return 0;
    }
    Enjoy.

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

  6. #6
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Thanks guys, I used it as a global variable first, but i've heard it said that globals should be used sparingly - if at all. However I find it annoying to pass them as parameters to the functions I need them to work with, i have like 6 or seven and yeah i have to write it only once in those functions but it is still redundant. know what i mean.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  2. Global objects and exceptions
    By drrngrvy in forum C++ Programming
    Replies: 1
    Last Post: 09-29-2006, 07:37 AM
  3. Global Variables, include files and classes
    By sharpstones in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2005, 10:06 AM
  4. Static global variable acting as global variable?
    By Visu in forum C Programming
    Replies: 2
    Last Post: 07-20-2004, 08:46 AM
  5. defining and using a global class
    By cjschw in forum C++ Programming
    Replies: 4
    Last Post: 03-05-2004, 09:51 PM