From 7e0f021a9aec35fd8e6725e87e3313b101d26f5e Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Sun, 27 Jan 2008 11:37:44 +0100 Subject: Initial import (2.0.2-6) --- reference/C/SYNTAX/static.htm | 175 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 175 insertions(+) create mode 100644 reference/C/SYNTAX/static.htm (limited to 'reference/C/SYNTAX/static.htm') diff --git a/reference/C/SYNTAX/static.htm b/reference/C/SYNTAX/static.htm new file mode 100644 index 0000000..273eb3a --- /dev/null +++ b/reference/C/SYNTAX/static.htm @@ -0,0 +1,175 @@ +static functions + + + + + + +
+
+

static functions

+
+
+

+ +static functions are functions that are only visable to other +functions in the same file. Consider the following code. +

+ + + + + +
+main.c +
+
+
+   #include 
+
+   main()
+   {
+     Func1();   
+
+     Func2();
+   }
+   
+
+
+ + + + + + +
+funcs.c +
+
+
+   /*************************************
+    *
+    * Function declarations (prototypes).
+    *
+    *************************************/
+
+   /* Func1 is only visable to functions in this file. */   
+
+   static void Func1(void);
+
+   /* Func2 is visable to all functions. */
+
+   void Func2(void); 
+
+   /*************************************
+    *
+    * Function definitions
+    *
+    *************************************/
+       
+   void Func1(void)
+   {
+     puts("Func1 called");
+   }
+   
+   /*************************************/
+   
+   void Func2(void)        
+   {
+     puts("Func2 called");
+   }
+   
+
+
+

+If you attempted to compile this code with the following +command, +

+ +

+ + + + +
+
+
+   gcc main.c funcs.c   
+
+
+
+

+it will fail with an error simular to..... +

+ +

+ + + + +
+
+
+   undefined reference to `Func1'  
+
+
+
+

+Because 'Func1' is declared as static +and cannot be 'seen' by 'main.c'. +

+


+

Notes:

+For some reason, static has different +meanings in in different contexts. +
    +
  1. When specified on a function declaration, it makes the +function local to the file. +

    +

  2. When specified with a variable inside a function, it +allows the vairable to retain its value between calls to +the function. See +static variables. +
+It seems a little strange that the same keyword has such +different meanings.... + +

+


+

See Also:

+ + +static variables

+ + + +C++ extensions for static

+ + + +

+ +


+

+

+ + + + +
+ Top + + Master Index + + Keywords + + Functions +
+
+

+


+
Martin Leslie +

+ + -- cgit v1.2.3-54-g00ecf