Skip to content

Latest commit

 

History

History
36 lines (23 loc) · 919 Bytes

C-lang.md

File metadata and controls

36 lines (23 loc) · 919 Bytes

C Programming

Declaration informs the compiler about the existence of a symbol. Definition allocates space.

In terms of automatic and register variables a declaration is also a definition: by declaring a variable with syntax type indentifier (i.e int var), space is allocated according to the variable type.

If no initial value is supplied, the content of that variable is gonna be garbage.

Structure

External variables

An external variable is accessible globally by name, it is initialized by default to 0.

  • must be defined exactly once outside of any function to allocate space.
  • must be declared in each function (or file) that needs it.
// file1.c

int var1; //global definition 

void fun1() {
    extern int var1; //local declaration
}

From K&R

The usual practice is to collect extern declarations in a header file that is included at the front of each source file