Skip to content
Snippets Groups Projects
Commit 8369cd3d authored by Mactavish's avatar Mactavish
Browse files

update c program structure

parent 56926324
Branches
No related tags found
No related merge requests found
...@@ -359,7 +359,6 @@ Objects declared in file scope have `static` storage duration. The lifetime of t ...@@ -359,7 +359,6 @@ Objects declared in file scope have `static` storage duration. The lifetime of t
`Allocated` storage is allocated and deallocated through library functions on requests, using dynamic memory allocation functions. `Allocated` storage is allocated and deallocated through library functions on requests, using dynamic memory allocation functions.
A concrete example of these storage durations: A concrete example of these storage durations:
```c ```c
...@@ -557,3 +556,82 @@ Using the header guard prevents the function definition of `func` being included ...@@ -557,3 +556,82 @@ Using the header guard prevents the function definition of `func` being included
A common practice when picking the identifier to use as a header file guard is to use the salient parts of the file path, filename, and extension, separated by an underscore and written in all capital letters. E.g. `FOO_BAR_BAZ_H` for a file located in `foo/bar/baz.h`. A common practice when picking the identifier to use as a header file guard is to use the salient parts of the file path, filename, and extension, separated by an underscore and written in all capital letters. E.g. `FOO_BAR_BAZ_H` for a file located in `foo/bar/baz.h`.
There are other ways of using the preprocessor directives and macros, this [article](https://en.wikibooks.org/wiki/C_Programming/Preprocessor_directives_and_macros) and [GCC documentation](https://gcc.gnu.org/onlinedocs/cpp/) provide extensive information about them. There are other ways of using the preprocessor directives and macros, this [article](https://en.wikibooks.org/wiki/C_Programming/Preprocessor_directives_and_macros) and [GCC documentation](https://gcc.gnu.org/onlinedocs/cpp/) provide extensive information about them.
## C Program Structure
We've talked about storage duration above. Storage duration and linkage are closely related. In C, you can use the **storage-class specifiers** to specify the storage duration and linkage of an object or a function, they are:
- `auto`: automatic duration and no linkage
- `register`: automatic duration and no linkage; address of this variable cannot be taken (we won't cover this, it's quite rare )
- `static`: static duration and internal linkage
- `extern`: static duration and external linkage
### Linkage
Linkage refers to the ability of an **identifier (variable or function)** to be referred to in other scopes.
C provides three kinds of linkage:
- `none`: The identifier can be referred to only from the scope it is in.
- `external`: The identifier can be referred to from everywhere in the program. (E.g. from other source file).
- `internal`: The identifier can only be referred to within the translation unit that contains the declaration.
There are some implicit rules if no storage-class specifier is provided, the defaults are:
- `extern` for all functions
- `extern` for all objects at file scope
- `auto` for objects at block scope
Let's look at some examples.
```c
// flib.h
#ifndef FLIB_H
#define FLIB_H
void f(void); // function declaration with external linkage
extern int state; // variable declaration with external linkage
static const int size = 5; // definition of a read-only variable with internal linkage
enum { MAX = 10 }; // constant definition
#endif // FLIB_H
```
```c
// flib.c
#include "flib.h"
static void local_f(int s) {} // definition with internal linkage (only used in this file)
static int local_state; // definition with internal linkage (only used in this file)
int state; // definition with external linkage (used by main.c)
void f(void) { local_f(state); } // definition with external linkage (used by main.c)
```
```c
// main.c
#include "flib.h"
int main(void)
{
int x[MAX] = {size}; // uses the constant and the read-only variable
state = 7; // modifies state in flib.c
f(); // calls f() in flib.c
}
```
#### Special Use of `static`
Declaring a variable at block scope as static creates an identifier with no linkage, but it does give the variable static storage duration:
```c
#include <stdio.h>
void foo() {
static int count = 0; // count has no linkage but has static storage duration
printf("Function has been called %d times\n", ++count);
}
int main() {
foo();
foo();
foo();
return 0;
}
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment