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/CONCEPT/storage_class.html | 219 +++++++++++++++++++++++++++++++++ 1 file changed, 219 insertions(+) create mode 100644 reference/C/CONCEPT/storage_class.html (limited to 'reference/C/CONCEPT/storage_class.html') diff --git a/reference/C/CONCEPT/storage_class.html b/reference/C/CONCEPT/storage_class.html new file mode 100644 index 0000000..884728e --- /dev/null +++ b/reference/C/CONCEPT/storage_class.html @@ -0,0 +1,219 @@ +C Storage Classes + + + +
+
+

C Storage Classes.

+
+
+

+C has a concept of 'Storage classes' which are used to define the +scope (visability) and life time of variables and/or functions. +

+So what Storage Classes are available? +

+ + + +
auto +register +static +extern +typedef +
+

+


+

auto - storage class

+auto is the default storage class for local variables. +
+	{
+	    int Count;
+	    auto int Month;
+	}
+
+ +The example above defines two variables with the same storage class. +auto can only be used within functions, i.e. local variables.

+


+

register - Storage Class

+register is used to define local variables that should be stored +in a register instead of RAM. This means that the variable has a maximum size +equal to the register size (usually one word) and cant have the unary '&' +operator applied to it (as it does not have a memory location). +
+	{
+	  register int  Miles;
+	}
+
+Register should only be used for variables that require quick access - such +as counters. It should also be noted that defining 'register' goes not mean +that the variable will be stored in a register. It means that it MIGHT be stored +in a register - depending on hardware and implimentation restrictions.

+


+

static - Storage Class

+ +Click here for static functions +

+static is the default storage class for +global variables. The two +variables below (count and road) both have a static storage class. +

+

+ + + + +
+
+
+     static int Count;
+     int Road;
+
+     main()
+     {
+       printf("%d\n", Count);
+       printf("%d\n", Road);
+     }
+
+
+
+

+ +'static' can also be defined within a function. If this is done, the variable +is initalised at compilation time and retains its value between calls. +Because it is initialsed at compilation time, the initalistation value +must be a constant. +This is serious stuff - tread with care. +

+

+ + + + +
+
+
+     void Func(void)
+     {
+       static Count=1;
+     }
+
+
+
+

+Here is an example

+ + +There is one very important use for 'static'. Consider this bit of code. +

+

+ + + + +
+
+
+     char *Func(void);
+
+     main()
+     {
+       char *Text1;
+       Text1 = Func();
+     }
+
+     char *Func(void)
+     {
+       char Text2[10]="martin";
+       return(Text2);
+     }
+
+
+
+

+'Func' returns a pointer to the memory location where 'Text2' starts +BUT Text2 has a storage class of auto and will disappear +when we exit the function and could be overwritten by something else. The +answer is to specify: +

+

+ + + + +
+
+
+     static char Text[10]="martin";
+
+
+
+

+The storage assigned to 'Text2' will remain reserved for the duration if the +program. + +


+

extern - storage Class

+extern defines a global variable that is visable to ALL object +modules. When you use 'extern' the variable cannot be initalized as +all it does is point the variable name at a storage location that has +been previously defined. +
+
+
+ 	Source 1				Source 2
+        --------				--------
+
+
+	extern int count;			int count=5;
+
+        write()					main()
+        {					{
+          printf("count is %d\n", count);	  write();
+        }					}
+
+ +Count in 'source 1' will have a value of 5. If source 1 changes the +value of count - source 2 will see the new value. Here are some example +source files. +

+Source 1
+Source 2

+ +The compile command will look something like.

+

+	gcc source1.c source2.c -o program
+
+ +
+

See Also:

+ +Data types. + + +

+ +


+

+

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

+


+
Martin Leslie +

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