Thread: about STATIC function

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    85

    Question about STATIC function

    Any C expert out there could explain in detail about what different between STATIC function and
    non-STATIC function?
    For example in the file, Display.c, below

    #include <stdio.h>
    #include "display.h"
    /* prototypes */
    static void showA(); /*this is static function*/
    void showB(); /*this is non-static fun.*/

    /* function definition */
    static void showA()
    {
    printf("Hola, I am from the Moon!");
    }
    void showB()
    {
    printf("Hello!, I am from Mercury planet");
    }
    Thank you in advance.
    DV007

  2. #2
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    ShowA because it is static can ony be call from a function inside Display.c. Because ShowB is nonstatic it can be called from any module or source file in the project.

    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    A static function can only be used in the source file that it was defined in. The static keyword means that it has file scope and cannot be used externally. A function has external linkage by default, so the programmer has to take action if they want it to be invisible to outside forces.

    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    85
    Thanx Sean+Prelude but talking about code efficience are they both the same???
    The return address of both functions are allocated on STACK segment when called? or static function is on global segment (BSS segment I guess), and non-static function is on the STACK?
    Again, thanx
    Last edited by dv007; 07-19-2002 at 01:53 PM.

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    85
    Anyway, you guys's help are faster than speed of
    light
    DV007

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >but talking about code efficience are they both the same???
    There may be an increase in efficiency when a function has file linkage, but I would imagine that in any practical sense they would be the same.

    -Prelude
    My best code is written with the delete key.

  7. #7
    Unregistered
    Guest
    I am not an expert but I think both static and non-static functions
    are allocated on STACK segment. Are you confuse with static
    and non-static data. Static variable is put on Data segment while
    non-static local variable is on STACK, but not apply to function.

    BUD-WISER

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I am not an expert but I think both static and non-static functions are allocated on STACK segment.
    Correct, somewhat. The functions themselves are not popped onto the stack, activation records for them are. An activation record is a data structure which contains all of the information needed to invoke a function and maintain the call chain for previous functions. Also, the activation records may not even be on the stack. It's far more efficient to keep as much as possible of the activation record in registers. But for most practical uses, you can consider all functions themselves to be placed on the stack.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. [GLUT] Pointers to class methods
    By cboard_member in forum C++ Programming
    Replies: 13
    Last Post: 02-16-2006, 04:03 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM