Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Supplying value
Agenda
Embedded systems
Why C?
ANSI C
Embedded SW
Design
SV I IP HVAC
Embedded Systems
Why C?
ANSI C Basic & Extended Syntax Elements
Embedded Software Architecture & Design
Coding Guidelines
Embedded Systems
Embedded systems
Why C?
ANSI C
Embedded SW
Design
Embedded Systems
Introduction
SV I IP HVAC
Embedded Systems
Embedded systems
Why C?
ANSI C
Embedded SW
Design
SV I IP HVAC
Embedded Systems
Embedded systems
Why C?
ANSI C
Embedded SW
Design
Typical applications:
Automotive applications
Aerospace applications
Defense systems
Mobile phone systems
Medical equipment
Household equipment
SV I IP HVAC
etc.
Embedded Systems
Embedded systems
Why C?
ANSI C
Embedded SW
Design
SV I IP HVAC
Embedded Systems
Embedded systems
Why C?
ANSI C
Embedded SW
Design
SV I IP HVAC
Embedded Systems
Embedded systems
Why C?
ANSI C
Embedded SW
Design
SV I IP HVAC
Embedded Systems
Embedded systems
Why C?
ANSI C
Embedded SW
Design
SV I IP HVAC
Embedded Systems
Embedded systems
Why C?
ANSI C
Embedded SW
Design
SV I IP HVAC
Hard Real-Time:
Missing a deadline has catastrophic results
for the system
Firm Real-Time
Missing a deadline causes an
unacceptable quality reduction
Soft Real-Time
Reduction in system quality is acceptable
Deadlines may be missed and can be
recovered from
Non Real-Time
No deadlines have to be met
Embedded Systems
Embedded systems
Why C?
ANSI C
Embedded SW
Design
SV I IP HVAC
Why C?
Embedded systems
Why C?
ANSI C
Embedded SW
Design
Why C?
SV I IP HVAC
Why C?
Embedded systems
Why C?
ANSI C
Embedded SW
Design
SV I IP HVAC
Why C?
Embedded systems
Why C?
ANSI C
Embedded SW
Design
SV I IP HVAC
Why C?
Embedded systems
Why C?
ANSI C
Embedded SW
Design
SV I IP HVAC
Why C?
Embedded systems
Why C?
ANSI C
Embedded SW
Design
SV I IP HVAC
Why C?
Embedded systems
Why C?
ANSI C
Embedded SW
Design
SV I IP HVAC
Why C?
Embedded systems
Why C?
ANSI C
Embedded SW
Design
SV I IP HVAC
ANSI C
Embedded systems
Why C?
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
ANSI C
ANSI C Introduction
Embedded systems
Why C?
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
Common C
There was one dominant form of the C
language.
This was the native UNIX form, which for
historical reasons is known as either Bell Labs
C, after the most popular compiler, or K.&R. C,
after the authors of the most popular textbook
on the language.
ANSI C Introduction
Embedded systems
Why C?
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
SV I IP HVAC
ANSI C
The American National Standards Institute
defined a standard for C, eliminating much
uncertainty about the exact syntax of the
language.
ANSI C Identifiers
Embedded systems
Why C?
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
Lexical convention
Meaning of Identifiers
ANSI C Identifiers
Embedded systems
Why C?
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
ANSI C Identifiers
Embedded systems
Why C?
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
Identifiers:
a sequence of letters and digits
functions, structures, unions and enumeration
members of structures or unions, enumeration
constants, typedef names and objects
Rules:
The first character must be a letter
The underscore _ counts as a letter
Upper and lower case letter are different
Identifiers may have any length , and for internal
identifiers, at least the first 31 characters are
significant
ANSI C Identifiers
Embedded systems
Why C?
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
ANSI C Identifiers
Embedded systems
Why C?
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
Constants:
integer-constant
character-constant
floating-constant
enumeration-constant
ANSI C Identifiers
Embedded systems
Identifiers:
Why C?
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Object (variable):
Program flow
SV I IP HVAC
ANSI C Identifiers
Embedded systems
Why C?
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
ANSI C Identifiers
Embedded systems
Why C?
ANSI C
Introduction
Storage classes
determines the lifetime of the storage associated
with the identified object ("global" or "local" lifetime)
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
Type:
determines the meaning of the value found in the
identified object
ANSI C Identifiers
Embedded systems
Why C?
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
Storage Class
automatic: objects are local to a block and are
discarded on exit from the block. Declaration within
a block create automatic objects if no storage class
specification is mentioned, or if the auto specifier is
used
static: objects may be local to a block or external to
all blocks, but in either case retain their values
across exit from and reentry to functions and blocks
ANSI C Identifiers
Embedded systems
Why C?
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
ANSI C Identifiers
Embedded systems
Why C?
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
auto:
default storage class
local lifetime
visible only in the block in which it is declared
is not initialized automatically
must be initialized explicitly
ANSI C Identifiers
Embedded systems
Why C?
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
static:
global lifetime
visible only within the block in which it is declared
all module global variables must be declared as
static
initialized to 0 by default (however, DO NOT
COUNT ON THIS!!!)
ANSI C Identifiers
Embedded systems
Why C?
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
register:
used for optimizations for heavily used variables
the variable is assigned to a high-speed CPU
register (rather than an ordinary memory location), if
possible
limitation: you cannot generate pointers to them
using the & operator
mostly obsolete, modern compilers do an excellent
job without such hints
ANSI C Identifiers
Embedded systems
Why C?
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
extern:
reference to a variable with the same name defined
at the external level in any of the source files of the
program
the identifier declared has its defining instance
somewhere else
used to make the external-level variable definition
visible within the block
is visible only in the block in which it is declared
ANSI C Identifiers
Embedded systems
Why C?
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
extern:
ANSI C Identifiers
Embedded systems
Type specifiers:
Why C?
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
void
specifier
char
short
int
float
struct-or-union
double
signed
unsigned
enum specifier
typedef-name
long
ANSI C Identifiers
Embedded systems
Type qualifiers
Why C?
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Syntax:
type-qualifier :
const
volatile
Preprocessor
Embedded SW
Design
SV I IP HVAC
may be used alone (as the sole declarationspecifier), or in conjunction with type specifiers
relevant only when accessing identifiers as l-values
in expressions
ANSI C Identifiers
Embedded systems
Why C?
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
const:
the object may be initialized, but not thereafter
assigned to
announces objects that may be placed in read-only
memory, and perhaps to increase opportunities for
optimization
volatile:
directs the compiler not to perform certain
optimizations on an object because that object can
have its value altered in ways beyond the control of
the compiler
ANSI C Identifiers
Embedded systems
Why C?
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
volatile:
useful for controlling access to memory-mapped
device registers, as well as for providing reliable
access to memory locations used by asynchronous
processes
ANSI C Identifiers
Embedded systems
Why C?
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
ANSI C Identifiers
Embedded systems
Why C?
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
program:
consists of a sequence of functions which mostly
are placed in different modules
module:
a group of functions and data . Large program can
be divided into a collection of separately compiled
modules. They can be placed in a library for use by
different programs
function:
consists of a function header and a function block
The function header contains the function name and
the function parameters.
The function block can contain a definition part and
a statement part
ANSI C Identifiers
Embedded systems
Why C?
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
block structure:
The curly brackets { and } are used for combining
definitions and statements to one block.
Definitions and statements are terminated by a ;.
A single ; represents a statement
In a function block further blocks can be nested
SV I IP HVAC
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
Numbers
Variables
Memory class modifier
External variables
Scope rules
Static variables
Register variables
Initialization
Data Types
Data encapsulation with
struct
Data encapsulation with
union
Bitfields
Enumerated data types
Definition of private types
Type Conversion
Twos complement
Nonnegative integers from 0 to (2k-1-1) are represented
in binary the same way as with sign-magnitude, 0
followed by a (k-1) - bit absolute value.
Negative integers -(2k-1) to -1 are represented by
adding 2k and expressing the result in binary.
25 =
-25 =
Preprocessor
Embedded SW
Design
SV I IP HVAC
Twos complement
Algorithm for Negating a Number:
Complement (flip) each bit.
Add 1
Complementing a k-bit number is same as subtracting
it from 2k-1.
25 :
1s compl.
2s compl.
Preprocessor
Embedded SW
Design
SV I IP HVAC
Constants
Text constants
Numerical constants
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Text constants
char
Ex: A, E, 9, #
string
Ex: Siemens
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
Text constants
Ex:
S
53
i
69
e m e
65 6D 65
n
6E
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
s \0
73 00
SV I IP HVAC
Numerical constants
Numbers always start with a digit.
The base is characterized with
0xnnnn hexadecimal number (digits: 0-9, a-f or A-F),
0nnn octal number (digits: 0-7),
0bnnnn binary number (digits: 0,1). - this is not ANSI
standard but many compilers accept this.
Any other number is supposed to be decimal.
SV I IP HVAC
Numerical constants
Long integer constants are specified with an ending L,
e.g. long a = 10L;
Unsigned integer constants are specified with an ending
u, e.g. unsigned int c = 3u;
Floating point values should contain a decimal point,
e.g. float b = 1.;
and may be written with mantissa and exponent,
e.g. float c = 0.5e-3;
The suffixes may be omitted. However, this might
sometimes result in an error.
Identifiers
Numbers, Variables
Expressions
Ex:
Program flow
Preprocessor
Embedded SW
Design
SV I IP HVAC
SV I IP HVAC
Variables
Variables are characterized by their:
value
address
attribute (modifier)
type
lifetime
Functions
Preprocessor
Declaration syntax:
[class] [modifier] [type] <name> [, <name1>][,]
Embedded SW
Design
SV I IP HVAC
Variables
The modifier can be one of:
volatile
const
Numbers, Variables
Expressions
Volatile
volatile <type> <name>
must always be read from its original location and is
not kept in a register at any time
compiler optimizations are not available, therefore it
must be used only if necessary
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
Const
const <type> <name>
allows only read access to the variable
may only be initialized once in a program. The
initialization is performed in the startup code
allows the compiler to perform type checking
Preprocessor
Embedded SW
Design
SV I IP HVAC
SV I IP HVAC
SV I IP HVAC
External variables
recommended use:
SV I IP HVAC
Variable scope
the part of the program within which the variable can
be used
For automatic variables declared at the beginning of
a function, the scope is the function in which the
name are declared
Local variables of the same name in different
functions are unrelated
The same is true of the parameters of the function,
which are in effect local variables
The scope of an external variable or function lasts
from the point at which it is declared to the end of
the file being compiled
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
Static variables
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
Register variables
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
SV I IP HVAC
Variable initialization
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
Before simulation:
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
Example 1
SV I IP HVAC
Before simulation:
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
Example 2
SV I IP HVAC
SV I IP HVAC
Data Types
Basic types : char, int, float, double
Type definition is used for:
the definition of the range of values
the size of memory
the operations allowed
the scaling of pointers
SV I IP HVAC
Data types:
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
Syntax:
struct [struct_name]
{
data_type1 ivar_list1;
data_type2 ivar_list2;
...
} [svar_list];
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
SV I IP HVAC
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
SV I IP HVAC
Before simulation:
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
union [union_name]
{
data_type1 ivar_1;
data_type2 ivar_2;
...
} [uvar_list];
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
SV I IP HVAC
SV I IP HVAC
SV I IP HVAC
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
SV I IP HVAC
Bitfields
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
Bitfields
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
Bitfields
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
Bitfields
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
SV I IP HVAC
Bitfields - example
struct TxIC
{
unsigned int glvl: 2;
unsigned int ilvl: 4;
unsigned int ie: 1;
unsigned int ir: 1;
unsigned int : 0;
} t7ic;
t7ic.ilvl = 12;
The compiler will assign the bits as given below:
SV I IP HVAC
Bitfields - example
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
Syntax:
enum [enum_name]
{
value_1 [= ival]
[,value_2 [= ival]]
...
} [enum_list];
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
Type Conversion
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
SV I IP HVAC
Type Conversion
Implicit cast
all char and short operands are converted to int
if one operand is unsigned, the other operand is
converted to unsigned as well
all float operand are converted to double
if the operand of an expression are of different types,
calculations always occur with the widest type.
(Width of a data type simply means the number of
bytes a value occupies.
the result of an expression is always adjusted to the
type of variable the result has.
Type Conversion
Implicit cast example
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
char v1;
int v2;
double v3;
Program flow
Arrays & Pointers
v2 = v1+v3
/*expression is double, result is int*/
v1 = v2 - 2*v1 /* expression is int. The result
variable is of type
char. The most significant
part of result is lost */
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
Type Conversion
Explicit cast
to avoid hidden code which might lead to wrong
results, every type conversion must be casted
explicitly.
Expressions
Program flow
SV I IP HVAC
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
Type Conversion
SV I IP HVAC
Variable assignment
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
SV I IP HVAC
Expressions
[unary operator] operand [binary operator][operand][];
unary operator: concern a single operand only
+ positive sign
- negative sign
++ increment
-- decrement
& address of
* indirection
sizeof(name) size of name in byte
(type cast)
explicit type casting
! logical negation
~ bit by bit inversion
Binary operators
performs a two operand operation
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
arithmetic operators
+ sum
- difference
* multiplication
/ division
% modulo operation
SV I IP HVAC
Binary operators
comparison operators
< less than
<= less or equal
> greater than
>= greater or equal
== equivalence
!= not equal
&&logical AND
|| logical OR
Binary operators
Why C?
bit-by-bit operations
& AND
| OR
^ XOR (exclusive OR)
<< shift left
>> shift right
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
SV I IP HVAC
&
^=
Expressions
ternary operator: performs a three operand operation
Introduction
Identifiers
Numbers, Variables
?:
conditional
Expressions
operand:
constant
variable
pointer
return value of a function
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
Shift operations
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
Shift operations
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Examples:
1. z = x * 2; ==>
2. z = x / 2; ==>
z = x << 1;
z = x >> 1;
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
SV I IP HVAC
Shift operations
SV I IP HVAC
Operators Hierarchy
SV I IP HVAC
Operators Hierarchy
Operators
Examples:
Incrementing before the calculated value is used:
x=++n;
Incrementing after the calculated value is used:
x=n++;
Preprocessor
Embedded SW
Design
x = ++ (x+y); x = 10++
SV I IP HVAC
not allowed
Operators
Examples:
x = x | MASK;
are set
n = ~ n;
negation of bits
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
Operators
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
Operators
Operators
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
Logical expressions
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
SV I IP HVAC
Sequences
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
SV I IP HVAC
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
Identifiers
Numbers, Variables
Expressions
Program flow
Preprocessor
Embedded SW
Design
SV I IP HVAC
SV I IP HVAC
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
The while-Statement
while ( expression )
sequence
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
SV I IP HVAC
The for-Statement
for ( [init_list] ; [expression]; [continue_list] )
sequence
init_list is a list of statements separated by commas
which will be executed unconditionally in advance of
the loop.
expression results in a boolean value TRUE or
FALSE. As long as it is TRUE, the sequence is
executed, followed by the statements of the
continue_list.
continue_list is a list of statements separated by
commas which are evaluated as long as the
expression is TRUE.
The for-Statement
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
The do - while-Statement
do
sequence
while ( expression );
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
ANSI C
Introduction
Identifiers
Numbers, Variables
goto
continue
break
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
SV I IP HVAC
SV I IP HVAC
Arrays
Pointers
Pointers scaling
Pointers and Function Arguments
Pointers and Arrays
Pointers vs.. Multi-dimensional Arrays
Functions
Function Header
Function Body
Calling a function
Arrays
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Functions
Preprocessor
Array Declaration:
<type> <name> [<size>];
Embedded SW
Design
SV I IP HVAC
Arrays
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
SV I IP HVAC
Arrays
Examples :
int t [4] ;
int j [] = {1, 2, 3 } ;
int k [5] = {1,2};
/* size 3 */
/* other elements are
initialized to 0 */
float t[2][3] = {{2.3, 4.6, 7.2}, {4.9, 5.1, 9.3}};
int l [2] = {1, 2, 3 } ;
/* Incorrect */
char vocals[5] = {a, e, i, o, u };
char string[] = This is a string;
Arrays
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
Pointers
Pointers
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
unsigned u16Val;
unsigned *p = &u16Val;
.
p is 0x0186 (address of
u16Val)
*p is 0x55AA; (value
located at address
0x0186, the value of
u16Val)
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
Pointers
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
Pointers
Pointers
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
Pointers scaling
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
SV I IP HVAC
Pointers scaling.
Example:
long *sl_ptr = 0x4000;
char *sc_ptr;
sc_ptr++ = (char *)(sl_ptr + 2);
will result in
tmp = sl_ptr + 2*sizeof(long)
= 0x4000 + 2*4
= 0x4008 ==> sc_ptr
sc_ptr++ = 0x4008 + 1*sizeof(char)
= 0x4008 + 1*1
= 0x4009 ==> sc_ptr
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
SV I IP HVAC
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
array[2]
array[1]
array[0]
s \n \0
g \n \0
e \n \0
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
array[2]
array[1]
array[0]
s \n \0
g \n \0
C o
e \n \0
Pointer to Strings
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
Pointer to Strings
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
Pointers
Functions
A function is defined by
a header (name, type, formal parameters)
a body
A function has
a scalar type (char, int, long or pointer) : actual
function
void type : procedure
Embedded SW
Design
SV I IP HVAC
Function Header
Syntax:
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
Functions
<visibility<type><name>(<parameters list>)
Type
void for procedure
scalar type (default int)
Visibility
static modifier used to hide the function
SV I IP HVAC
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
Delimited by { and }
Allows local declarations
Automatic data located on the stack
Static data located in global memory
If type is not void, the function includes a return
statement which affects the result
the return exits the function
Input parameters are used as they have been
declared
Output parameters are used using the *
operator
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
SV I IP HVAC
Functions - Example
Function with input parameter (example):
int f (int x)
{
return x+1 ;
}
Calling:
int a;
const b = 14 ;
int r;
a=3;
r = f (a) ; /* r is set to 4 */
r = f(b+r-1)
/* r is set to 18 */
Functions - Example
Function with input-output parameter (example):
void g (int *y, int x)
{
*y = x+1 ;
}
Calling:
int a; const b = 14 ; int r;
Preprocessor
a=3;
g(&r,a) ; /* r is set to 4 */
g(&r,b+r-1)
/* r is set to 18*/
g(r,a) ; /* incorrect, protection fault */
Embedded SW
Design
SV I IP HVAC
SV I IP HVAC
Functions - Example
Function bad example:
Function with input parameter as an output parameter
void g (int y, const int x)
{
y = x+1 ;
}
Calling:
int a; const b = 14 ; int r;
r=0;
a=3;
g(r,a) ; /* r is not affected */
g(&r,a) ; /* compilation error */
Pointer to Functions
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
Pointer to Functions
ANSI C
type (*name)();
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
int comp_res;
comp_res = (*comp)();
...
}
int main()
{
extern int strcmp(), numcmp();
if(numerical)
sort(lineptr, nlines, numcmp);
else
sort(lineptr, nlines, strcmp);
}
SV I IP HVAC
Preprocessor directives
Embedded systems
Why C?
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
Preprocessor directives
Preprocessor directives
Embedded systems
Why C?
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
Macro definition
File inclusion
Conditional compilation
Memory model
Preprocessor directives
Embedded systems
Why C?
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
Preprocessor directives
Embedded systems
Why C?
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
#define
introduces the definition of a preprocessor
macro
#undef
clears a preprocessor macro definition
#include
includes the source text of another file
#if
evaluates an expression for conditional
compilation.
#ifdef
checks for conditional compilation whether a
macro is defined
Preprocessor directives
Embedded systems
Why C?
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
#ifndef
checks for conditional compilation whether a
macro is not defined
#elif
introduces an alternative #if branch following
a not compiled #if, #ifdef, #ifndef or #elif
branch.
#else
introduces an alternative branch following a
not compiled #if, #ifdef, #ifndef or #elif
branch.
#endif
completes a conditional compilation branch
Preprocessor directives
Embedded systems
#line
indicates the line number and, optionally, a
file name which is used in error logging files
to identify the error position.
Why C?
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
#error
Preprocessor directives
Embedded systems
#pragma
Why C?
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
Preprocessor directives
Embedded systems
Why C?
ANSI C
Macro definitions
#define Macro_Name [ [(parameters)] replace_text]
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
Preprocessor directives
Embedded systems
Why C?
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
Preprocessor directives
Embedded systems
Why C?
Files inclusion
#include <file_name>
#include file_name
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
Preprocessor directives
Embedded systems
Why C?
Files inclusion
ANSI C
Introduction
Identifiers
Numbers, Variables
Expressions
Program flow
Arrays & Pointers
Functions
Preprocessor
Embedded SW
Design
SV I IP HVAC
Embedded SW Design
SV I IP HVAC
Disciplined SW Development
Partitioning
Real Time SW Systems
Closer Look to a Real Example
Embedded SW Design
Disciplined SW Development
SV I IP HVAC
Embedded SW Design
Disciplined SW Development
Main target: produce quality SW
Quality is exactly what the customer says it is
SW quality is the reason we need processes
SW maturity can be measured according to CMM
(Capability Maturity Model)
SV I IP HVAC
Embedded SW Design
SV I IP HVAC