Thread: scope and life time of function

  1. #1
    Registered User
    Join Date
    Jul 2018
    Posts
    81

    scope and life time of function

    I have mostly seen that the variable has a life time and scope.

    But does any function also have life time and scope ?

    Code:
    #include<stdio.h>
    
    void fun (void)
    {
        printf("sleep \n");
    }
    
    
    int main(void)
    {
        fun();
        return 0;
    }
    Code:
    #include<stdio.h>
    
    static void fun (void)
    {
        printf("sleep \n");
    }
    
    
    int main(void)
    {
        fun();
        return 0;
    }
    what the static function means in second code ?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > But does any function also have life time and scope ?
    Their scope depends on whether you use the word 'static' or not.
    Their lifetime is the life of the program - excluding the case of functions loaded via dynamic link libraries.

    > what the static function means in second code ?
    It limits who can call the function to only those functions also declared in the same source file.

    Imagine you're creating a library containing several methods, which happen to make use of some utility functions.
    You would make those utility functions static, so they can't be accessed by mistake by your library users.
    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.

  3. #3
    Registered User
    Join Date
    Jul 2018
    Posts
    81
    Quote Originally Posted by Salem View Post
    > But does any function also have life time and scope ?
    > what the static function means in second code ?
    It limits who can call the function to only those functions also declared in the same source file.
    .
    Thanks for making clear. A static function is accessible only within the file it is defined in. if we want to access from the another file then we have to make function extern

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by vajra11 View Post
    Thanks for making clear. A static function is accessible only within the file it is defined in. if we want to access from the another file then we have to make function extern
    Or, declare a pointer to a function and initialize this pointer to the static function:

    Code:
    /* module1.c */
    static int f( int x ) { return x + x; }  // visible to this module only;
    int (*fptr)(int) = f;  // global extern ptr to function f;
    Code:
    /* module2.c */
    extern int (*fptr)(int);
    ...
    int main( void )
    {
      int a = fptr(2);  // calling the module2.c static function, indirectly.
      ...
    }
    Last edited by flp1969; 02-18-2020 at 12:18 PM.

  5. #5
    Registered User
    Join Date
    Oct 2013
    Posts
    24
    Thanks @flp1969...Never thought that function pointer can be useful in that case too..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. life time of variable ?
    By Player777 in forum C Programming
    Replies: 6
    Last Post: 12-01-2019, 08:49 AM
  2. Pointer life & scope
    By 6tr6tr in forum C++ Programming
    Replies: 7
    Last Post: 04-24-2008, 10:56 AM
  3. scoped lock and object life time
    By pheres in forum C++ Programming
    Replies: 0
    Last Post: 01-22-2008, 02:25 AM
  4. system.map create/modify during linux life-time
    By pavmarc in forum Linux Programming
    Replies: 2
    Last Post: 08-17-2005, 09:59 PM
  5. A lesson in life kids: Bed time is for your own good!
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 11-06-2003, 02:27 PM

Tags for this Thread