summaryrefslogtreecommitdiff
path: root/reference/C/CONCEPT/storage_class.html
diff options
context:
space:
mode:
authorTobias Klauser <tklauser@distanz.ch>2008-01-27 11:37:44 +0100
committerTobias Klauser <tklauser@xenon.tklauser.home>2008-01-27 11:37:44 +0100
commit7e0f021a9aec35fd8e6725e87e3313b101d26f5e (patch)
treeb1cacc4b24393f517aeb4610e9e1021f954307a8 /reference/C/CONCEPT/storage_class.html
Initial import (2.0.2-6)2.0.2-6
Diffstat (limited to 'reference/C/CONCEPT/storage_class.html')
-rw-r--r--reference/C/CONCEPT/storage_class.html219
1 files changed, 219 insertions, 0 deletions
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 @@
+<title>C Storage Classes</title>
+<head>
+</head>
+<body bgcolor="#ffffcc">
+<center>
+<hr>
+<h1>C Storage Classes.</h1>
+<hr>
+</center>
+<p>
+C has a concept of '<i>Storage classes</i>' which are used to define the
+scope (visability) and life time of variables and/or functions.
+<p>
+So what Storage Classes are available?
+<p>
+<table border=2 bgcolor=ivory>
+<tr>
+<td><a href="#auto">auto</a>
+<td><a href="#register">register</a>
+<td><a href="#static">static</a>
+<td><a href="#extern">extern</a>
+<td><A HREF="../SYNTAX/typedef.html">typedef</A>
+</tr>
+</table>
+<p>
+<hr>
+<h2><a name="auto">auto - storage class</h2>
+<b>auto</b> is the default storage class for local variables.
+<pre>
+ {
+ int Count;
+ auto int Month;
+ }
+</pre>
+
+The example above defines two variables with the same storage class.
+auto can only be used within functions, i.e. local variables. <p>
+<hr>
+<h2><a name="register">register - Storage Class</h2>
+<b>register</b> 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).
+<pre>
+ {
+ register int Miles;
+ }
+</pre>
+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.<p>
+<hr>
+<h2><a name="static">static - Storage Class</h2>
+
+<a href="../SYNTAX/static.htm">Click here for static functions</a>
+<p>
+<b>static</b> is the default storage class for
+<a href="../SYNTAX/glo_int_vars.html#global">global variables</a>. The two
+variables below (<b>count</b> and <b>road</b>) both have a static storage class.
+<p>
+<center>
+<table border=2 bgcolor=ivory width="50%">
+<tr>
+<td>
+<pre>
+
+ static int Count;
+ int Road;
+
+ main()
+ {
+ printf("%d\n", Count);
+ printf("%d\n", Road);
+ }
+</pre>
+</td>
+</tr>
+</table>
+</center>
+<p>
+
+'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.
+<p>
+<center>
+<table border=2 bgcolor=ivory width="50%">
+<tr>
+<td>
+<pre>
+
+ void Func(void)
+ {
+ static Count=1;
+ }
+</pre>
+</td>
+</tr>
+</table>
+</center>
+<p>
+Here is an <a href="../EXAMPLES/static.c">example</a><p>
+
+<a name="static2">
+There is one very important use for 'static'. Consider this bit of code.
+<p>
+<center>
+<table border=2 bgcolor=ivory width="50%">
+<tr>
+<td>
+<pre>
+
+ char *Func(void);
+
+ main()
+ {
+ char *Text1;
+ Text1 = Func();
+ }
+
+ char *Func(void)
+ {
+ char Text2[10]="martin";
+ return(Text2);
+ }
+</pre>
+</td>
+</tr>
+</table>
+</center>
+<p>
+'Func' returns a pointer to the memory location where 'Text2' starts
+BUT Text2 has a storage class of <a href="#auto">auto</a> and will disappear
+when we exit the function and could be overwritten by something else. The
+answer is to specify:
+<p>
+<center>
+<table border=2 bgcolor=ivory width="50%">
+<tr>
+<td>
+<pre>
+
+ static char Text[10]="martin";
+</pre>
+</td>
+</tr>
+</table>
+</center>
+<p>
+The storage assigned to 'Text2' will remain reserved for the duration if the
+program.
+
+<hr>
+<h2><a name="extern">extern - storage Class</h2>
+<b>extern</b> 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.
+<pre>
+
+
+ Source 1 Source 2
+ -------- --------
+
+
+ extern int count; int count=5;
+
+ write() main()
+ { {
+ printf("count is %d\n", count); write();
+ } }
+</pre>
+
+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.
+<p>
+<a href="../EXAMPLES/extern1.c">Source 1</a><br>
+<a href="../EXAMPLES/extern2.c">Source 2</a><p>
+
+The compile command will look something like.<p>
+<pre>
+ gcc source1.c source2.c -o program
+</pre>
+
+<hr>
+<h2>See Also:</h2>
+<img src="../../GRAPHICS/whiteball.gif">
+<a href="data_types.html">Data types.</a>
+
+
+<p>
+
+<hr>
+<p>
+<center>
+<table border=2 width="80%" bgcolor="ivory">
+<tr align=center>
+<td width="25%">
+<a href="../cref.html"> Top</a>
+</td><td width="25%">
+<a href="../master_index.html"> Master Index</a>
+</td><td width="25%">
+<a href="../SYNTAX/keywords.html"> Keywords</a>
+</td><td width="25%">
+<a href="../FUNCTIONS/funcref.htm"> Functions</a>
+</td>
+</tr>
+</table>
+</center>
+<p>
+<hr>
+<address>Martin Leslie
+</address><p>
+</body>
+</html>