16.05.2015 Views

Working with the Unix OS

Working with the Unix OS

Working with the Unix OS

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

C Programming – Basics<br />

main()<br />

{<br />

int x;<br />

x = 1;<br />

{ int x;<br />

x = 2;<br />

{<br />

int x;<br />

x = 3;<br />

printf("x=%d\n",x);<br />

}<br />

printf("x=%d\n",x);<br />

}<br />

printf("x=%d\n",x);<br />

}<br />

Running this program will result in <strong>the</strong> following output:<br />

x=3<br />

x=2<br />

x=l<br />

Program Form<br />

Files to include<br />

Macro definitions<br />

Global variable<br />

declarations<br />

Function definitions<br />

Declarations of<br />

formal parameters<br />

Local variable<br />

declarations<br />

Function<br />

Body<br />

|#include ...<br />

|#define ...<br />

|--------------|<br />

|func1(....) |<br />

| |<br />

|{ |<br />

| |<br />

|} |<br />

|==============|<br />

|==============|<br />

|func2 (....) |<br />

|{ |<br />

| |<br />

|} |<br />

|==============|<br />

|main(...) |<br />

|{ |<br />

|} |<br />

|==============|<br />

Storage Classes<br />

As well as having a type, C variables have a class which describes how <strong>the</strong>y are stored in memory. There are four<br />

storage classes:<br />

Automatic<br />

This is <strong>the</strong> default storage class. Memory is allocated for an automatic variable when <strong>the</strong> block in which it<br />

is declared is entered and this storage is deallocated when <strong>the</strong> block is exited. This is equivalent to <strong>the</strong><br />

normal Pascal local variable.<br />

Register<br />

This is <strong>the</strong> same as for automatic except that if possible <strong>the</strong> compiler will attempt to use a hardware register<br />

for storing <strong>the</strong> variable making access faster. Most compilers find it too hard to do.<br />

Static<br />

Memory is allocated for a static variable at compile time and is never deallocated. Local static variables<br />

retain <strong>the</strong>ir values between function calls. Similar to <strong>the</strong> SAVE facility in FORTRAN.<br />

External<br />

Equivalent to global variables in Pascal. They are not local to any function including main.<br />

Variables declared outside <strong>the</strong> scope of any function are global variables. A function may access any global<br />

variable declared above it in <strong>the</strong> source code <strong>with</strong>out any fur<strong>the</strong>r declarations.<br />

If however <strong>the</strong> programmer chooses to declare <strong>the</strong> global variable again <strong>with</strong>in a function as an external variable,<br />

its global declaration may appear below <strong>the</strong> function in <strong>the</strong> source code.<br />

Storage class descriptions appear before <strong>the</strong> type in a variable declaration.<br />

26

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!