Beruflich Dokumente
Kultur Dokumente
A Quick Start
Mohamed Shalan, PhD
C Quick STart
Agenda
C Crash Course
C Traps & Pitfalls
Optimization
C Quick STart
C Crash Course
History of C
Structure of a C program
C data types
Variable declaration and scope
C operators
Loops and iterations
Pointers
C Quick STart
History of C
C Quick STart
Program Structure
C Quick STart
Four modifiers
char: character
int: integer
float: real or floatin point
double: double precision float
signed
unsigned
long
Short
Additionally, C supports
C Quick STart
C Quick STart
Qualifiers
C Quick STart
C Quick STart
C Operators
C Quick STart
10
C Quick STart
11
Pointers (1)
C Quick STart
12
Pointers (2)
C Quick STart
13
Arrays
C Quick STart
14
Multidimensional Arrays
3
...
2 2 2
2 2 2
2 2 2
10
Mohamed Shalan, PhD
C Quick STart
15
Multidimensional Arrays
C Quick STart
16
Function Pointer
Without typedef:
int (*f(double)) (char *);
With typedef:
typedef int (*Pfunc) (char *);
Pfunc f(double);
C Quick STart
17
C Typedef
int (*func2)(void)
use
C Quick STart
18
C Structures
b->x = 5;
Dont forget to initialize b!
C Quick STart
19
C Unions
C Quick STart
20
4
Mohamed Shalan, PhD
C Quick STart
21
b
Pad
a
b
c
C Quick STart
a
b
c
22
Bit Manipulation
void set_bit (int *a, int bit)
{
(*a) |= (1<<bit);
}
void clear_bit (int *a, int bit)
{
(*a) &= ~(1<<bit);
}
C Quick STart
23
a
b
c
d
e
C Quick STart
24
Bit Fields
struct student_struct {
unsigned idnum : 7;
unsigned year : 2;
unsigned gender : 1;
unsigned credit : 1;
} student;
Only int data types (unsigned or signed, but not long) may be used in
the bit field.
This structure will probably take up 16 bits, not 11, due to the need to
align data types in appropriate memory addresses (usually multiples of
8 bits, e.g. 16, 32, 64).
WARNING: Bit fields will save space, but access will probably be very
slow. If you need compactness and speed, you will probably want to
use bit shift operators on built-in data types instead.
C Quick STart
25
C Quick STart
26
C Quick STart
27
Free
malloc(
C Quick STart
28
Conditional compilation
#ifdef __STDC__
C Quick STart
29
C Quick STart
30
Macro:
C Quick STart
31
C Quick STart
32
Expands to 5 + 3 * 2 + 4
Operator precedence evaluates this as 5 + (3*2) + 4 = 15 not
(5+3) * (2+4) = 48 as intended
Moral: By convention, enclose each macro argument in
parenthesis:
C Quick STart
33
Inline Assembly
In both cases
C Quick STart
34
C Quick STart
35
Code instructions
Outputs maps results of instructions into C
variables
Inputs maps C variables to inputs of
instructions
Clobbers tells the compiler to forget the
contents of some registers
C Quick STart
36
GCC/ARM Interrupts
void __attribute__ ((interrupt("IRQ")))
tc0_cmp (void);
{
timeval++;
VICVectAddr = 0;
C Quick STart
37
C Quick STart
38
C Language Traps
= is not ==
& and | are not && or ||
printf(\n); vs printf ("\n");
if (flags & FLAG != 0)
r = h<< 4+l;
if (x[i] > big);
big = x[i];
C Quick STart
39
C Language Traps
Dangling Else
if (x == 0)
if (y == 0) error();
else {
z = x + y;
f (&z);
C Quick STart
40
C Language Traps
Octal numbers
int numbers[] = { 001,
010,
014 };
// 8 not 10
// 12, not 14
Non-terminated comment
a=b; /* this is a bug
c=d; /* c=d will never happen */
C Quick STart
41
Language Traps
C Quick STart
42
Language Traps
C Quick STart
43
C Quick STart
44
Optimizations Tricks
C Quick STart
45
Optimizations Tricks
Global variables
C Quick STart
46
Optimizations Tricks
int f(void);
int g(void);
int errs;
void test1(void) { errs += f(); errs += g();
}
void test2(void) {
int localerrs = errs;
localerrs += f();
localerrs += g();
errs = localerrs;
}
Mohamed Shalan, PhD
C Quick STart
47
Optimizations Tricks
Can be written as
void InitPos2(Object *p) {
Point3 *pos = p->pos;
pos->x = 0;
pos->y = 0;
pos->z = 0;
}
Mohamed Shalan, PhD
C Quick STart
48
Optimizations Tricks
C Quick STart
49
Optimizations Tricks
C Quick STart
50
Optimizations Tricks
Loop Termination
C Quick STart
51
Optimizations Tricks
Loop Jamming
Function Looping
C Quick STart
52
Optimizations Tricks
Loop Unrolling
C Quick STart
53
Optimizations Tricks
Avoid using ++ and -- etc. within loop expressions. E.g.: while(n--){}, as this can
sometimes be harder to optimize.
Minimize the use of global variables.
Declare anything within a file (external to functions) as static, unless it is
intended to be global.
Use word-size variables if you can, as the machine can work with these better
Don't use recursion. Recursion can be very elegant and neat, but creates many
more function calls which can become a large overhead.
Avoid the sqrt() square root function in loops - calculating square roots is very
CPU intensive.
Single dimension arrays are faster than multi-dimension arrays.
Single precision math may be faster than double precision - there is often a
compiler switch for this.
Floating point multiplication is often faster than division - use val * 0.5 instead of
val / 2.0.
Addition is quicker than multiplication - use val + val + val instead of val * 3.
C Quick STart
54
Optimizations Tricks
P4
PXA250
C Quick STart
55