Professional Documents
Culture Documents
Remeber
Life has no limitations except the ones you make
Course Objective
Writing programs and algorithms using C. Programming microcontrollers. Applying the embedded C technique.
Contents
Contents
Introduction Variables and Constants Conditional Statements Operator Loops Arrays, Strings, and Pointers Nonprimitive Data types Function and Macros Selected Topics in embedded C
Introduction
Why C language ?
C is a programming language that allows a software engineer to efficiently communicate with a computer.
Standards
K&R ANSI C Brian Kernighan and Dennis Ritchie 1983 the American National Standards Institute began the standardisation process 1989 the International Standards Organisation continued the standardisation process 1990 a standard was finalised
ISO C
Standard C
Embedded C ?
Embedded Systems is not only a SW stack
SW
HW
Why embedded C
Embedded SW engineer
Portability Optimization Quality Readability Complexity HW Compatibility HW knowledge Min. Resources
Compilation process
Editor
Its used to edit the C code
Compiler
It used to convert our C code to the assembly code. Then, the assembler convert it from assembly to machine code.
Linking
The final stage
linking the object code to code libraries This stage produces an executable program, which is named a.out under linux by default.
IDE
Integrated development environment ALL in One solution.
Welcome to C programming
#include
Its handled in the pre-processing operation. Its used to add the contents of the header file to the *.c file. #include <header_file.h>
Search the include path
#include header_file.h
Search the current directory then the include path
main() function
The entry point. The return type is int.
The default return type in C.
int main(void){ .. }
Hello
#include <stdio.h> /* comment */ int main(void) { printf("Hello\n"); printf("Welcome to the Course!\n"); return 0; }
Variables
Life time
Variable
address
Value
Type
Name
variables
Variables are memory storage (placeholder) for values that might change during program life time.
Variables
Must start with a character or an underscore only. From second letter and on, it can be a character, a digit, or an underscore. Shall not take the name of language reserved keywords (if, for, while, etc...) It is case sensitive.
Name
Variables
Life time
Stack heap
Scope
x c2 c
y b2 b
Variables
Life time
Scope
: Example 1:
Whats the life time and scope of the variables in example 1 ?
Variables
Data Type
Char Int
1 byte Used to hold a character This data type is used to represent integer values. Int = 1 word processor specific
Data Types
float/double
Fractional numbers double is double
Data Type
Standard length
Data Type
Data Type
typedef keyword.
typedef unsigned int unit16;
Readability
portability
Variables
Value
Signed Unsigned const
Variables address
Pointers Register
Remember
Group 1 Qualifier/Modifier extern, typedef, auto, register, static signed, unsigned short, long const volatile Default auto
2 3 4 5
Example
short unsigned volatile const int a=5;
Operators
Operators
Arithmetic
+ Addition - Subtraction * Multiplication / Division % Mod (remainder) ++ Increment -- Decrement
Operators
Relational
> Greater than < Less than >= Greater than or equal <= Less than or equal != Not equal == equal
Operators
Logical
&& Logical And || Logical Or ! Logical Not
Bit wise
| bitwise Or & bitwise And ~ bitwise Not ^ bitwise Xor
Example
a = 1 /*0000 0001*/ b = 2 /*0000 0010*/
(a && b) -> will evaluate to TRUE
Operators
Shift
>> <<
Ex.
j = 0x00000008*/
Operators Precedence
Example 4
HELP ME !!!
i = 10;
(!i == 5) Vs. (i != 5)
x = x op y
Functions
Functions
Function is a block of reusable code.
type function_name(arg_list);
Recursive functions
Function Calls itself
int factorial(int n) { int result; /*This is called the termination condition*/ if(l == n) { return (1); } /* recursive call */ result = factorial(n-l) * n; return(result); }
Conditional Statements
If/else
Note
Example
If( a >= 100) { /* Do action1*/ { elseif(a >= 50) { /* Do Action2*/ } else { /*the default action */ }
Usage of if-else
Switch
-How to make it ? -Whats the condition ? -Whats break ? -Whats default ?
example
switch(litre_count) { case 0: printf(empty\n"); break; case 1: printf(1 litre only \n"); break; case 2: printf(Stop filling the container\n"); break; default: printf(system down!\n"); break; }
?
The ternary operator
Var = (condition) ? Value_if_true : value_if_false;
Example:
Result = (x > 0) ? 5 : -4 ;
Loops
for loop
Used to repeat a certain task n numbers of times
For loop
How does it work? Whats optional ?
example
X 2 3
Y 1 1 1 1
4 5
6
7
1
0
For loop
If(you know the number of iterations) { decision = use for loop; } else { decision = use while; }
while
Do.. while
Compare
Continue
Return
Arrays
Arrays
Arrays
An array is a collection of data items (called elements) all of the same type C always allocates the array in a single block of memory The size of the array, once declared, is fixed
Arrays
int a[5] = { 10, 20, 30, 40, 50 }; double d[100] = { 1.5, 2.7 }; long n[50] = { 0 }; ===================================== Whats wrong with this code ->
int i = 7; const int c = 5; int a[i]; double d[c];
Arrays
const int SIZE = 10; int i; int group1[SIZE]; /*first group of numbers*/ int group2[SIZE]; /*second group of numbers*/ int group3[SIZE]; /*result of summation*/ /*Code that fills group1 and group2 with values*/ for(i = 0; i < SIZE; i++) { group3[i] = group1[i] + group2[i]; }
Strings
Strings
char courseName1[100] = "ANSI C"; char courseName2[100] = {'A', 'N', 'S', 'I', ' ', 'C', '\0'}; printf(courseName1); printf(courseName2);
Structure
Setup your template Create your instance Access the instance members
Structures
allows the user to define his/her own data types
struct tire { int int char* };
Struct Car { struct tire m_frontTire; struct engine m_Engine; int colour; };
Structure Initialization
struct Tire testTire = {10, 100, "Zooba"};
struct Tire testTire; testTire.m_pressure = 10; testTire.m_diameter = 100; testTire.m_brand = "Zooba"; (testTire.m_pressure)++; printf("The tire pressure = %d\n", testTire.m_pressure);
Quizzzzzzzz
Unions
union of one or more variable of one or more data types that shares the same space.
union testUnion { int a; /*occupy 4 bytes*/ char b; /*occupy 1 byte (shares the first byte of the variable a)*/ };
Enumeration
data type is designed for variables that contain only a limited set of values.
Ex.
days of a week
Dont forget typedef enum week_day today = MONDAY; week_day today = SUNDAY;
Pointers
Pointers
normal variables hold values, pointer variables hold memory addresses int * x ; /* Pointer to integer*/
- size of the pointer = size of integer - x value is the integer address - Ex.
char c = 'Z'; char* p = &c; *p = 'A';
Pointers
int *sokkaree; long int *p; int* pf; char c, d, *pc int* pd, e, f; char* start;
Pointers
we use an '*' right after the type to declare a pointer variable '&' is called the address-of operator, and it is used to get the address of its operand. Note:
Array name with no index is a pointer to the 1st element in the array Pointers cant be used with register variable
Pointers Example
(*p)++
*p Find value ()++ increment value
Discussion
Answer
a) int a; // An integer b) int *a; // A pointer to an integer c) int **a; // A pointer to a pointer to an integer d) int a[10]; // An array of 10 integers e) int *a[10]; // An array of 10 pointers to integers f) int (*a)[10]; // A pointer to an array of 10 integers g) int (*a)(int); // A pointer to a function a that takes an integer argument and returns an integer h) int (*a[10])(int); // An array of 10 pointers to functions that take an integer argument and return an integer
MACROS
Macros
All preprocessors has to be preceded by a '#' character
#include #define #if , #else , #elif , #ifdef #undef
#endif
Examples
Conditional compilation This is very useful if you're writing programs that run on different platforms, for example Linux and Windows
#define TEST 1 int main() { #if TEST printf("Test is set\n"); /*Will only be compiled in if TEST is nonzero*/ #else printf("Test is reset\n"); /*Will only be compiled in if TEST is zero*/ #endif return 0; }
Examples
#define MAX_LENGTH 5 Difference between #if , #ifdef , #ifndef
This is a macro definition that takes one argument and returns the absolute value of it.
Example
Check the difference between the different preprocessing operations.
General Discussion
Compare
Structures Vs. Arrays. Function like macro Vs. Inline function.
Thank you