Table of Contents
C Notes
Operators in C and C++ – Wikipedia
C Standard Library Reference Tutorial
gcc compiler
-g
– option adds debug symbols
An Introduction to GCC – How the compiler works
The sequence of commands executed by a single invocation of GCC consists of the following stages:
- preprocessing (to expand macros)
- compilation (from source code to assembly language)
- assembly (from assembly language to machine code)
- linking (to create the final executable)
Compilation is a multi-stage process involving several tools, including the GNU Compiler itself (through the gcc or g++ frontends), the GNU Assembler as, and the GNU Linker ld. The complete set of tools used in the compilation process is referred to as a toolchain.
Variable Scopes
- Orange – Global Scope
- White – Fine Grained Scope
- Pink / yellow – Local Scope
static
keyword
Global Variable
Scopes variables and function to be local to the file they are contained in. They can not be access and named could be duplicated between files.
Local Variable
Remembers value set between function calls. Variable value is not destroyed at the end of a function like it normally would be
Header files
Include guard
Protects against a header file being loaded twice
#ifndef _EXAMPLE_H_INCLUDED_
/* File contents */
#define _EXAMPLE_H_INCLUDED_
#endif /* end _EXAMPLE_H_INCLUDED_ */
Variables
Volatile
Embed with Elliot: The Volatile Keyword | Hackaday
Tells the compiler to not optimize a variable out and they it could change at any point in time.
volatile uint32_t blah;
- Useful for global variables.
- Should also be used when declaring variable for loops that are only used for delays
volatile uint8_t i;
for (i=0; i < 200; i++) {
;
}
Constant
Makes a variable unchangable variable must have a value set at declarations
const float blah = 2.45
Integers
The header file stdint.h that comes with your compiler defines a bunch of integer types that tell you how big they are and if they are signed or not. Our first bit of C code looks like this:
#include < stdint.h >
uint32_t unsigned32BitInteger;
Floating point
In IEEE 754, the float data type, also known as single precision, is a 32-bit value that gives you a range of ±1.18×10−38 to ±3.4×1038 and about 7 digits of precision. That means that you can only accurately represent pi as 3.141592. That’s fewer digits than you might expect.
Not good enough for you? Well the double data type, also known as double precision is a 64-bit value with a range of ±2.23×10−308 to ±1.80×10308 and almost 16 digits of precision. Our value of pi can be 3.141592653589793.
typedef float float32_t;
typedef double float64_t;
Floating point constants are assumed to be doubles (64-bit).
typedef
used to define a new type
typedef struct {
int x;
int y;
} point;
point p;
Strings
Structures
struct point {
int x;
int y;
}
struct point p;
p.x = 10;
p.y = 5;
draw(p);
To assign a value to a element in a structure use
structure.element = value
Structure and Pointers
(*structure).element
– De-reference element in struct pointer
&(*structure).ptr_element
– de-reference address of pointer element in struct pointer
When using a pointer to a structure, to access the members of a structure, you must use the ->
operator as follows:
struct_pointer->title;
enum
typedef enum {
CAN_250000_BAUD = 0,
CAN_500000_BAUD = 1,
CAN_1000000_BAUD = 2,
} CAN_BAUD_T;
Control flow and logic
Loop directives
break
– breaks out of a loop
continue
– skip to next run
Switch / Case
switch (var) {
case value1: // note the colon after the case value
statement;
statement;
break;
case value2:
statement;
statement;
break;
default:
statement;
statement;
break;
}
Conditional operator
Operator: Conditional
Symbol: ?:
Form: c ? x : y
Operation: If c is nonzero (true) then evaluate x, otherwise evaluate y
The conditional operator (?:) (also known as the “arithmetic if” operator) is C++’s only ternary operator (it takes 3 operands). Because of this, it’s also sometimes referred to as the “ternary operator” (we suggest you avoid calling it this in case C++ adds another ternary operator in the future).
The ?: operator provides a shorthand method for doing a particular type of if/else statement.
If/else statements in the following form:
if (condition)
expression;
else
other_expression;
can be rewritten as:
(condition) ? expression : other_expression;
Pointers and memory allocation
#coding/c #7-notes