ch4 - Lec1 - Basic C Programming Structure
ch4 - Lec1 - Basic C Programming Structure
Interfacing
Ch4_lec1_ Basic C
programming Structure
Kassahun Tamir 1
Outline
C Programming IDEs
Micro-controller header files
Compiler data types
Different types of variables
Arithmetic operations
Bit parallel logical operations
Shift and relational operations
Structures
✗ If-then-else
✗ Switch
✗ While and do while
✗For
Functions, Arrays and Pointers
Kassahun Tamir 2
C Programming IDEs
We interconnect the Hardware/Electronics parts using hardware
simulators such as, Proteus
Kassahun Tamir 3
Micro-controller header files
An embedded program in its simplest form:
Header Files,
Macros, and
Function and
Global Variable Declaration
Configuring Registers
Local Variables Declaration
Loop Forever
Kassahun Tamir 4
Different Types of Variables
A variable is declared by the reserved word indicating its type
and size followed by an identifier.
Kassahun Tamir 5
Arithmetic Operations
Binary Arithmetic Operators:
+ - * / %
Unary Operators:
++ --
Kassahun Tamir 6
Bit Parallel Logical Operations
Logical Operators:
&& AND
|| OR
! NOT
Bit-wise Operators:
& Bit-wise AND
| Bit-wise OR
^ Exclusive OR (XOR)
~ Complement
>> Shift to the Right
<< Shift to the Left
Kassahun Tamir 7
Shift and Relational Operations
Shift Operators:
>> Shift Right
<< Shift Left
Relational Operators:
==
!=
<
>
<=
>=
Kassahun Tamir 8
Control Structures: If-then-else
The “if” selection statement:
Kassahun Tamir 9
Control Structures: If-then-else
Nested “if…else” statements:
Kassahun Tamir 10
Control Structures: switch case
Kassahun Tamir 11
Functions
Why Functions?
Reusability
Kassahun Tamir 12
Functions
Function Definition:
Function Definition
Example:
Function Call:
Kassahun Tamir 13
Arrays: One Dimensional Arrays
Defining Arrays:
Kassahun Tamir 14
Arrays: One Dimensional Arrays
Examples:
Kassahun Tamir 15
Arrays: One Dimensional Arrays
Using character arrays to store and manipulate
Strings
Assign String values to character arrays
Kassahun Tamir 16
Arrays: One Dimensional Arrays
Passing Arrays to Functions:
They are passed by
reference implicitly
Kassahun Tamir 17
Arrays: Multidimensional Arrays
Kassahun Tamir 18
Pointers
A variable that holds a memory address
Kassahun Tamir 19
&, * and Declaring variables of pointer type
& is used to reference the memory address of a
variable
Kassahun Tamir 23
Pointers and Arrays
When we pass the entire array as an argument to
functions, functions get complete access to the
original array
✗ Means that it directly affects the original value
Kassahun Tamir 24
Pointer to Constants
const int *ptr = &count // int const* ptr; also possible
✗ (*ptr) = 10; //error
✗ (*ptr) ++; //error
✗ in j = (*ptr)+56; //OK as we are not modifying count
Kassahun Tamir 25
Pointers to Functions
Pointers can be used as a function argument
void myFunc(int *ptr);
In the function body, use ptr, as if it has been properly
defined and initialized
To call the aforementioned function:
myFunc(&count);
We may also pass pointer to int(not address of pointer
to int)
Int count=20, *m;
m = &count;
myFunc(m)
Kassahun Tamir 26
Example 1
Kassahun Tamir 27
Example 2
Kassahun Tamir 28
Questions?
Kassahun Tamir 29