Sunday, March 12, 2023

Notes on C

 Function is a callable block in assembly.  It means the block is marked with a label at the beginning so it can be jumped to symbolically.

Function defined with implementation details placed before (function) main served double up as a function declaration (signature that other function can call accordingly).

The main function is called by one of the exec family call and value is return to exec call when the C program ends.

Command line can be passed to the main function as argument.  The argument are passed as an array of string.  It can be declared as char *argv[] or char **argv.

There is no boolean type in C.  Integer (int) is used to represent false with a value of 0.  1 or any positive value represent true.  While (1) represent an infinity loop with a always true condition.

Condition used in control statement (for, while, do, switch etc) must be enclosed by parenthesis.

For SWITCH statement, execution will start from a matching case statement and continue from there straight until it reach a BREAK or end of SWITCH.  In other words, it will also execute codes in ensuing cases that following the matching case.

Array variable is a type of pointer.  It points to the first item of an array.  So is a function name.  It can be passed as a pointer as an argument to another function.

Pointer arithmetic convert an offset to its product with the item size.  For example, arr+3 = arr+(3*4) for an array of integer.  The compiler will perform the offset arithmetic on behalf of human.

There are 4 storage classes.  extern and static are for both function and variable.  auto and register are applicable to variable only.  

Variable is defined when it is assigned a value.  Variable is declared when no value is assigned.  There can only be 1 definition statement for variable in a program but there could be many declaration.

int a = 10; /*definition*/

int a; /*declaration*/

Variable defined outside a block is either static or extern (default).

Variable defined within a block is either register or auto (default).

When a variable is defined as extern or static, it exists through out the program's life time.  

A static variable declared in a function (block scope) is initialized once even the function may be called repeatedly.  A static variable has default value 0 if it is not initialized.

A static variable declared outside a function (outside a block) has scope from the point of declaration to the end of the file containing the declaration code.  It is not visible outside the file (i.e. in other program files that may be compiled together).

A extern variable has global scope if it is defined (initialized) in one file and is declared in other files that require access to the variable (and that are to be compiled together).

register class is typically not required anymore as compiler would optimize the code by assigning variables to register for efficiency.  If compiler deems no register is available, variable defined as register will be treated as auto.

volatile is not a storage class.  When variable is defined as volatile, the programmer informed the compiler to avoid optimize it by assigning it to register.  For example, if a variable is to be updated by ISR (e.g. placement for received byte), the variable needs to be in memory rather than register.

Function can be defined as static or extern, but not auto or register.  A static function is visible within the file containing the function.  It is similar to private function in C++,  A extern function is visible outside the file it is defined when the other file declares the function.