0% found this document useful (0 votes)
9 views22 pages

C Programming Essentials for Embedded Systems

C programming

Uploaded by

nour15073
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views22 pages

C Programming Essentials for Embedded Systems

C programming

Uploaded by

nour15073
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Embedded Systems

Workshop
C Programming

ENG. Zeyad Hesham


Global vs. Local variable:

Feature Global Variable Local Variable


Accessible throughout the entire Accessible only within the
Scope
program function/block
Declared In Outside all functions Inside a function or block
Exists as long as the program Exists only during function/block
Lifetime
runs execution
When multiple functions need to When data is used temporarily in
Use Case
share data a function
Occupies memory for the entire Released after function/block
Memory Usage
program duration ends
Can be modified anywhere (risky Safer, changes don't affect other
Modification
for bugs) parts
Decision Making Statements
• If Statement
• if execute statement or not
• if else choose on of two statements
• switch choose one of number of statements

• if (control expression) if (control expression)


{ {
Program statements Program statements 1
} }
else
{
Program statements 2
}

• Avoid trying compare real variables for equality


Decision Making Statements
• Conditional Operator
• Condition? Expression1 : Expression2 ;
• Ex. Grade>=50? Printf(“Pass \n”) :printf(“Fail \n”);
• If-else ➔ Conditional Operator
• if – else-if
• if – else if ……….. else
• Allow to check for more than 2 conditions.
• Switch Case
Switch (integer expression){
case 1:
statements;
break;
.
.
default:
default statements;}
Loops
• In C programming, there is often a need for repeating the same part of the code multiple
times.
• For definite repetition ➔ counter-controlled loops (for)
• For indefinite repetition ➔ sentinel-controlled loops (while)
• Syntax:
When Condition is
Loop Type Syntax Use Case
Checked

for (init; condition; update) Known number of


for loop Before loop starts
{//code} iterations

Unknown number of
while (condition)
while loop Before loop starts iterations (entry-
{// code}
controlled)

do {//code Always run at least once


do...while loop After loop runs once
} while (condition); (exit-controlled loop)
Loops
loops
Break and Continue

Feature break continue

Immediately exits the loop or Skips current iteration, continues


Purpose
switch loop

Used In for, while, do...while, switch for, while, do...while loops only

Ends the entire loop or switch


Effect Jumps to the next loop iteration
block

Transfers control outside the Transfers control to loop


Control Flow
loop condition/update

Exit on a condition (e.g., if (x == 5) Skip iteration (e.g., if (x == 5)


Typical Use
break;) continue;)
Break and Continue
Functions
• A function in C is a set of statements that, when called, perform some
specific tasks. It provides modularity and code reusability.

Term Description Example


Tells the compiler a function
Declaration int add(int, int);
exists (also called prototype)
Provides the actual code (body)
Definition int add(int a, int b) { return a + b; }
of the function
Call Executes the function int result = add(3, 4);
Variables in the function
Parameters int add(int a, int b) → a, b
definition
Values passed during the
Arguments add(3, 4) → 3, 4
function call
Functions

Feature Pass by Value Pass by Reference


The address (reference) of the
Definition A copy of the variable is passed
variable is passed
Changes do not affect the Changes affect the original
Effect on Original
original variable variable
Memory Used Uses more memory (copies) Uses less memory (no copies)
Syntax in C Normal variables Use pointers
Safer, as original data is Riskier, but allows direct
Safety
protected modification
Functions
• Pass by value • Pass by reference
Arrays
• What is an Array?
• An array is a collection of elements of the same data type, stored in
contiguous memory locations.

• Syntax: data_type array_name[size];


• Example: int numbers[5] = {1, 2, 3, 4, 5};

• Key Points:
• Accessed via index: numbers[0] to numbers[4]
• Index starts at 0
• Stored sequentially in memory
pointers
• What is a Pointer?
• A pointer is a variable that stores the address of another variable.

• Syntax: data_type *pointer_name;


• Example: int x = 10;
int *p = &x;
• Key Symbols:
• &x → address of x
• *p → value at the address p (dereferencing)
• Use Cases:
• Dynamic memory
• Function arguments (pass by reference)
• Working with arrays
Relationship Between Arrays and Pointers
• Array Name as Pointer:

• Equivalence:
• arr[i] is the same as *(arr + i)
• Array name is a pointer to the first element
• Note:
• You cannot reassign an array name like a pointer
• ( arr = ptr; //is invalid)
Arrays and Pointers in Functions
• Arrays are Passed as Pointers

• Call with array


Pointer to Char
• A pointer to char stores the address of a character or the first character of a
string.

• Important Notes:
• String literals like "Hello" are read-only — modifying them causes undefined
behavior.
• Use char array[] if you need a modifiable string.
• char * can be used with string functions like strlen, strcpy, etc.
Typedef Keyword
• What is typedef?
• typedef is used to create a new name (alias) for an existing data type to improve
readability and simplify complex declarations.

• Syntax: typedef existing_type new_name;

• Use Cases:
• Simplify complex pointer or structure syntax
• Improve code readability
• Create portable types across platforms
How to access memory mapped registers to control
peripherals?
• These create aliases for standard data types:
• uint8 = 8-bit unsigned (i.e., unsigned char)
• uint16 = 16-bit unsigned (i.e., unsigned short)
• Useful for embedded systems to improve portability and clarity.
• These macros define memory-mapped register access:
• 0x4F and 0x4C are hardware register addresses
• *(volatile uint8*)0x4F means:
• Cast 0x4F as a pointer to a volatile 8-bit unsigned integer
• Dereference it to access the register directly
• volatile tells the compiler not to optimize access to this memory (registers
may change unexpectedly, e.g., by hardware)
• #define creates a shortcut name to use in code like a normal variable:
THANK
YOU

You might also like