Thread: static void ?

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Belgium
    Posts
    60
    Rep Power
    12

    static void ?


    what is the difference between
    Code:
    void main( ...)
    and
    Code:
    static void main( ...)
    in C
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    Hmmm...I don't think that's a proper use of static. The static keyword is usually used with classes. A static variable or function exists even if no objects of the class exist, and all objects share the static variable or function. If you change a static member variable of an object, all the objects reflect that change. You could use a static member variable to keep track of how many objects of the class have been created.

    You can also use static variables inside regular functions. They continue to exist even after the function executes and all the local variables are destroyed. Sometimes they're used to count how many times the function was called.
    Last edited by 7stud; March 25th, 2003 at 03:34 AM.
  4. #3
  5. *bounce*
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Delft, The Netherlands
    Posts
    514
    Rep Power
    42
    He was talking about C, 7stud, not C++. C doesn't even have classes... You're right about the static variables though :)

    A static function's scope is limited to the file it's defined in; only functions in the same file can "see" (and hence call) it. Technically, what it does is inhibit the export of the function name to the linker.

    The static keyword is called a storage class, and a function has any one of the following three (excerpt from H&S):

    auto -- Permitted only in declarations of variables at the heads of blocks. It indicates that the variable has local (automatic) extent. (Because this is the default, auto is rarely seen in C programs.)

    extern -- May appear in declarations of external functions and variables, either at the top level or at the heads of blocks. It indicates that the object declared has static extent and its name is known to the linker.

    static -- May appear on declarations of functions or variables. On function definitions, it is used only to specify that the function name is not to be exported to the linker. On function declarations, it indicates that the declared function will be defined--with storage class static--later in the file. On data declarations, it always signifies a defining declaration that is not exported to the linker. Variables declared with this storage class have static extent (as opposed to local extent, signified by auto).
    Any questions? :D
    "A poor programmer is he who blames his tools."
    http://analyser.oli.tudelft.nl/
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Belgium
    Posts
    60
    Rep Power
    12
    so basicly a static function in C
    can only be used in the same *c file ?
    what is the use of this static function then ?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    272
    Rep Power
    19
    This behavior extends to data declared outside of any function as well. Static variables declared outside of a function can only be used by functions in that file. In Visual C++ declaring main as a static will have no effect since the compiler will ignore the static and just generate a warning.

    The fact that C makes everything global by default and requires that we use static to localize things is backwards in my opinion. We should have to explicitly make data and functions global in scope. Doing it the way C does just promotes bad programming practice.
    Last edited by 3dfxMM; March 25th, 2003 at 07:52 AM.
  10. #6
  11. *bounce*
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Delft, The Netherlands
    Posts
    514
    Rep Power
    42
    The use of static for functions and global variables is to avoid name clashes in the object code's symbol tables. This way, you can still have two or more functions with the same name.
    "A poor programmer is he who blames his tools."
    http://analyser.oli.tudelft.nl/
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    11
    Rep Power
    0

    void!??


    According ISO/IEC standard C++ of 1998 main is int.
    void is available old type.
    I never heared about static notification using with
    non-class member functions.
  14. #8
  15. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,225
    Rep Power
    2222

    Re: static void ?


    Originally posted by Jonas_G
    what is the difference between
    Code:
    void main( ...)
    and
    Code:
    static void main( ...)
    in C
    A quick note that hadn't been brought up yet.

    main() is the program's entry point and, as such, must be made known to the linker for the start-up code to be able to link to it. Therefore, one would never want to make main() static -- unless possibly to have a function in another module called "main", but that just seems a bit too weird.

    As an experiment, I made a program's main() function static and got this error from gcc:
    c:/fsf/lib\crt0.o(.data+0x92):crt0.s: undefined reference to `main'
    c:/fsf/lib/libc.a(crt1.o)(.text+0x312):crt1.c: undefined reference to `main'
    So it's not just a "would never want to", but also a "just plain won't work".

IMN logo majestic logo threadwatch logo seochat tools logo