Mastering C for Embedded Systems: A Complete Review

Jalal Sadigli

--

Declaring Variables

//type-qualifier(s) type-modifier data-type variable-name = initial value;

Data types include integer, floating point, enumerated, derived, void.

int apple = 1;                             //integer
float orange = 1.1; //Floating point
enum fruits {apple, orange, banana}; //Enumerated

int numbers[5] = {1,2,3,4,5}; //Array is a derived data type

struct Person{
int age;
float height;
}; //Struct is a derived data type

union Data{
int i;
float f;
}; //Union is a derived data type

int a = 100;
int *ptr = &a //Pointer is a dervied data type

The void data type specify that no value is present and it is mainly used with functions and pointers.

Type modifiers are for increasing the size of the data types or changing the properties of the variables. Type modifiers include short, long, unsigned, signed.

Type qualifiers are instructions to the compiler on how the variable should be managed. Type qualifiers include const, volatile, restrcit.

Data representation

Data is stored in memory in bits and 8 bits are 1 Byte.

In software data is represented in binary systems(0s and 1s). “0b” indicates data in binary.

Using hexadecimal numbers(base 16) is also an option. “0x” indicates data in hexadecimal.

Data is assigned, manipulated and compared using different operators which are grouped as logical, bitwise, arithmetic, and relational.

Logical operators

Logical operators are “||”, “&&”, “!”. Result of logical operators is Boolean. if it is zero it is false, and any non-zero result is true. Logical operators are mainly used in conditional blocks.

/* 
|| -> Logical "or"

&& -> Logical "and"

! -> logical "not"

*/

Bitwise operators

Bitwise operators are “<<”,“>>”, “|”, “&”, “^”, “~”.

/*
<< -> left shift
11101 << 1 = 11010

>> -> right shift
10111 >> 1 = 01011

| -> bitwise or
X Y X|Y
0 0 0
0 1 1
1 0 1
1 1 1

& -> bitwise and
X Y X&Y
0 0 0
0 1 0
1 0 0
1 1 1

^ -> bitwise xor
X Y X^Y
0 0 0
0 1 1
1 0 1
1 1 0

~ -> bitwise not
X ~X
0 1
1 0

*/

Arithmetic operators

Arithmetic operators are “+”, “-”, “/”, “*”, “++”, “ — “, “%”.

int a = 10;
int b = 3;

int c;

c = a + b; // 13
c = a - b; // 7
c = a / b; // 3
c = a * b; // 30
c = a % b; // 1

c = a++; // c equals a, a increased by 1
c = ++a; // a increased by 1, c equals new a

c = b--; // c equals b, b decreased by 1
c = --b; // b decreased by 1, c equals new b

Bitwise and arithmetic operators can be combined with assignment operator for ease of operation.

Relational operators

Relational operators are “< =”, “> =”, “<”, “>”, “==”, “!=” and they are mainly used in conditional blocks.

/*

<= -> less than or equal to
>= -> greater than or equal to
> -> less than
< -> greater than
== -> is it equal?
!= -> not equal

*/

Conditional Blocks

Conditional block are used for controlling program flow.

// if, if , else

if(condition a)
{
//code
}

if(condition b)
{
//code
}
else
{
//code
}

// if, else if, else

if(condition a)
{
//code
}

else if(condition b)
{
//code
}
else
{
//code
}

//switch

switch(expression)
{
case first-case-of-the-expression:
//code
break;
case second-case-of-the-expression:
//code
break;
default:
//code
break;

}

Loops

Loops are advanced control statement that allow a block of code to be repeated based on a condition.

// condition is preloop check 
for(initialize-variable; condition; update-expression)
{
//code
break; //Exit loop immediately
continue; //Moves to next iteration, ignoring the rest of the code for the current iteration
}

// condition is preloop check
while(condition)
{
//code
break; //Exit loop immediately
continue; //Moves to next iteration, ignoring the rest of the code for the current iteration
}

//condition is postloop check
do
{
//code
break; //Exit loop immediately
continue; //Moves to next iteration, ignoring the rest of the code for the current iteration
}while (condition);

--

--

No responses yet

Write a response