Thread: static variables confusion

  1. #1
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656

    static variables confusion

    static.h:
    Code:
    static int t = 4;
    static.c:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "static.h"
    
    /*int t=7; Does not work */
    int main(void) {
    	printf("%i\n", t);
    	return 0;
    }
    static2.c
    Code:
    int t=6;
    Shouldn't there be an error when compiling like this:
    Code:
    gcc static.c static2.c
    Because there should be conflicts with the 2 t integers.

    The output is 4

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    using static on a global object (be it a variable or a function) means to only link internally.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Seems OK to me, as far as it goes.

    Of course, the real problem would arise when you tried to access the t in static2.c from within static.c
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Ok, well I'll just try to be careful with what variables I choose to be static for the .c/.h duo files. I just needed to make sure it wasn't a concept I missed or something like that.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well I wouldn't normally initialise statics inside header files for starters.

    That kinda limits including that header file in ONE .c file, which is of limited use (though not entirely useless).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Header files have:
    -prototypes for functions
    -All global variables (?)
    -All #define macros (?)

    C Files:
    -Fill up the functions
    -Fill up the variables

    Am I going ok here? As standard philosophy for C programming, am I going in the right direction as far as programming structure?

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Am I going ok here?
    Looks okay to me. Of course, how you structure your modules would be dependent on the application, but for the general case your list is fine.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 23
    Last Post: 07-09-2007, 04:49 AM
  2. Static variables + Initialisation
    By kris.c in forum C Programming
    Replies: 2
    Last Post: 07-08-2007, 02:16 AM
  3. Static variables
    By ashughs in forum C++ Programming
    Replies: 3
    Last Post: 10-26-2006, 09:21 AM
  4. static variables
    By swgh in forum C++ Programming
    Replies: 2
    Last Post: 08-28-2006, 06:35 AM
  5. Replies: 5
    Last Post: 11-19-2002, 07:49 PM