C language
Contents
Things to remember about the C language if you want to keep sane
The stack
Symptoms:
- The program counter seems to jump around at random
- Corrupted variables and data.
Declarations
#define
volatile
The volatile keyword tells the compiler that the dollowing object is subject to sudden change in a way that is not described in the source code. For example a RS-232 data register that receive data from the outside. Volatile forces the compiler to generate the code that access the memory location each time instead of cache it in a register. Can be applied to any declaration.
Symptoms of missing volatile statements:
- Code fails when you enable compiler optimizations
- Code fails when interrupts or DMA/Hardware peripherials are enabled
extern
static
const
Read only. Can be applied to any declaration.
const volatile unsigned long int base_address = 0xFFFF;
Loop constructs
for() direction and signed while do while
switch(var) { case 0: //code break; }
if else
& | ^ ~
Shifts
- Right shift >> The effect on signed numbers are implementation defined. Arithmetic shift on ARM compilers.
- Left shift <<
- Rotate
&& || ==
!= > < >= <=
+= -=
- =
/= >>= <<=
Type specifiers
Data types (ARM C and C++)
- char 8 bits
- short 16 bits
- int 32 bits
- long 32 bits
- long long 64 bits (The low word of a long long is at the low address in little-endian mode, and at the high address in big-endian mode.)
- float 32 bits
- double 64 bits
- long double 64 bits
- All pointers 32 bits
- bool (C++ only) 32 bits
Memory alignement in bits is word length or 32 whichever is smallest
Other
- void
- signed
- unsigned
type casting subroutines void return global vs local
pointers
& (unsigned int *)
arrays multiple dimensions array pointers, 1d, 2d string, terminating character /n /r /t escape characters vs ""
- character and on what type of lines is belongs on
include files "file" <file>
.c
.h
included libraries