Embedded C .
1. C Language
1.1. Introduction
- Programming language
is a set of instructionsthatused tocommunicate with acomputer andcontrol its behavior. - C is a
general-purpose proceduralprogramming language.
1.2. Expression
- An expression
is a combination ofoperators, constants and variables that evaluate to a value. - There are
several types of expressions, including:- constant expr:
5, 10+3 - integral expr:
x*y - floating expr:
x + 10.75 - relational expr:
x <=y - logical expr:
x > y && z == 6 - pointer expr:
&x - bitwise:
x<<3
- constant expr:
1.3. Variables & Data Types
- A variable is
a named locationin memorythat stores data. Every variable must be declared to specify its type. e.g.int a; - Common Data Types in C:
| Data Type | Size (Typical) | Range |
|---|---|---|
| char | 1 byte | -128 to 127 |
| unsigned char | 1 byte | 0 to 255 |
| short | 2 bytes | -32,768 to 32,767 |
| int (16-bit) | 2 bytes | -32,768 to 32,767 |
| int (32-bit) | 4 bytes | -32,768 to 32,767 |
| unsigned int | 2 bytes | 0 to 65,535 (16-bit) |
| long | 4 bytes | -2,147,483,648 to 2,147,483,647 |
| float | 4 bytes | ±3.4e±38 (~6–7 digits precision) |
| double | 8 bytes |
On a 32-bit machine,
shortis not efficient because the CPU is designed to process 32-bit data in one step.
1.4. Operators
- An operator is
a symbolthatperforms an operation on value. - There are several operators, including:
- Arithmetic operators are used to perform arithmetic operations on operands, include
addition +,subtraction -,multiplication *,division /, andremainder % - Logical operators are used to combine two or more conditions into a single expression, include
and &,or |, andnot ! - Bitwise operators are used to perform bit-level operations on the operands, include
and &,or |,exclusive-or ^,negation ~,left shift <<, andright shift >> - Relational operators are used for the comparison of the two operands, include
equal ==,not equal !=,greater/e >/ >=, andless/e </<= - Assignment operators are used to assign value to a variable, include
= ,and=` - Casting operators convert one data to another
(new_type) operand; - Other:
& *…
- Arithmetic operators are used to perform arithmetic operations on operands, include
When adding/subtracting integer to pointer, the integer is multiplied by the size of the pointed data. e.g. long *p; p+1 adds 4 to the address value p. Thus, p[i] is an abbreviation of *(p+i)
1.5. Statements
- A statement
is a complete instructionthat the program executes.
An expression produces a value, but a statement performs an action.
- There are several kinds of statements, including:
- Expression statements
a=b - Condition statements
if(con), switch - Iteration statements
while(ter_con), for(init,ter_con,do_end_step), do{state} while(ter_con); - Compound statements
{ // list of statements } - Jump statements
goto, break, continue
- Expression statements
1.6. Data Structures
- Data structures
is a way of organizing datain memoryfor efficient use. It can be classified intotwo main types: built-in and user-definedBuilt-in data structuresincludes arrays, structures and unions.User-defined data structuresare implemented using structures and pointers.
1.6.1. Array
- An array
is a collectionof elementsof the same typestored in contiguous memory locations.
A multi-dimensional array is an array with more than one dimension, 2-D,3D etc.
1.6.2. Structure
- A structure
is a user-defined data typethat groups different types of data together. - Data alignment means placing data in memory
at addresses that match the CPU’s preferred boundaries. (short2 - others 4) - Padding is
extra unused memoryadded by the compilerto satisfy alignment requirements. - e.g.
// ======== Memory layout ========
// address content
// 0 a (1 byte)
// 1–3 padding (3B)
// 4–7 b (4 bytes)
struct Example {
char a; // 1 byte
int b; // 4 bytes
};
1.6.3. Union
- A union is like a structure, but its members
are allocated at the same addressand share the same memory area. - e.g.
// ======== Memory layout (union) ========
// All members share the same starting address
// address content
// 0–3 i (4 bytes)
// f (4 bytes)
// c (1 byte, but overlaps)
union Data {
int i; // 4 bytes
float f; // 4 bytes
char c; // 1 byte
};
enum Type {
INT_TYPE,
FLOAT_TYPE
};
struct TaggedData {
enum Type type; // tag
union {
int i;
float f;
} data;
};
1.6.4. Enum
- An enumeration (or enum) is a user defined data type that
contains a set of named integer constants.
1.7. Function
- A function
is a reusable block of codeused to perform a specific task. - A function pointer
is a pointer that stores the address of a function, allows indirect function calls. - A function prototype tells the compiler what the function looks like before it is actually defined.
int f(int, int *);
1.8. Preprocessing Directives
-
Declarations mean giving types to variables or functions, using keyword
extern. e.g.extern int x; -
Definitions mean defining the contents for variables or functions. e.g.
int x = 1; -
The keyword
externis used to declare a variable or function defined elsewhere. -
The keyword
staticis used to specifyinternal linkage at file scopeor toextend the lifetime of a variable inside a function.
2. C Embedded
2.1. Program Memory Model
- The memory layout of a program
shows how its data is storedin memory during execution. It typically include the following segments:Code (Text) Segment: stores executable code and is usually read-only.Data Segment:Initialized Data Segment: containsglobalandstaticvariablethat are initialized.Uninitialized Data Segment (BSS): storesglobalandstaticvariablethat are not initialized.
Heap Segment: is used for dynamic memory allocation.Stack Segment: is used for storing local variables, functions params, and return addresses for each function call.