Sie sind auf Seite 1von 55

C Language

A Quick Start
Mohamed Shalan, PhD

Mohamed Shalan, PhD

C Quick STart

Agenda

C Crash Course
C Traps & Pitfalls
Optimization

Mohamed Shalan, PhD

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

Mohamed Shalan, PhD

C Quick STart

History of C

Developed in 1972 by Dennis Ritchie on a DEC PDP11 at Bell


Systems Lab as a system development language

For many years the de-facto C standard was the version


provided with Unix System V

Derived from the language B of Ken Thompson, which itself was


based on BCPL, developed by Martin Richards

The C ProgramminLanguage, Brian Kernigham and Dennis


Ritchie, Prentice-Hall 1978

In 1983 ANSI creates a group to begin the standardization of


C

ANSI C is finalized in 1989, and ISO adopts it in 1990

Mohamed Shalan, PhD

C Quick STart

Program Structure

Mohamed Shalan, PhD

C Quick STart

Data Types (1)

Four basic data types

Four modifiers

char: character
int: integer
float: real or floatin point
double: double precision float

signed
unsigned
long
Short

Additionally, C supports

the null data type: void


Any user-defined types

Mohamed Shalan, PhD

C Quick STart

Data Types (2)

Four storage classes

auto: the default. Variables are automatically created and


initialized when they are defined and are destroyed at the
end of the block containing their definition. They are not
visible outside that block
register: a type of auto variable. a suggestion to the
compiler to use a CPU register for performance
static: a variable that is known only in the function that
contains its definition but is never destroyed and retains its
value between calls to that function. It exists from the time
the program begins execution
extern: a static variable whose definition and placement is
determined when all object and library modules are
combined (linked) to form the executable code file. It can
be visible outside the file where it is defined.

Mohamed Shalan, PhD

C Quick STart

Data Types (3)

Qualifiers

Const keyword indicates that memory once


initialized, should not be altered by a program.
volatile keyword indicates that the value in the
memory location can be altered even though
nothing in the program code modifies the
contents. Examples:

Memory-mapped peripheral registers


Global variables modified by an interrupt service routine
Global variables within a multi-threaded application

Mohamed Shalan, PhD

C Quick STart

Variable declaration and scope

Variables MUST be declared before they are used

Variables declared inside a block are local to that block

Any declaration MUST precede the first statement in a block


They cannot be accessed from outside the block

Variables can be initialized when they are declared or


afterwards

Mohamed Shalan, PhD

C Quick STart

C Operators

Mohamed Shalan, PhD

C Quick STart

10

Loops and Conditions

In C any expression different than ZERO is TRUE, including


negative numbers, strings,
C provides the following constructs

Mohamed Shalan, PhD

C Quick STart

11

Pointers (1)

A pointer is a variable that stores


a memory address
A pointer must be declared and
initialized before it can be used
Pointers and arrays are closely
related

the name of the array serves as a


pointer to its first element
the first element has index 0
array elements can be addressed
using brackets or pointer arithmetic

Strings of characters and arrays


are closely related

A string is an array of characters


followed by the \0
null character

Mohamed Shalan, PhD

C Quick STart

12

Pointers (2)

Pointers can point to C


functions

The pointer will point to the


memory address that stores the
first instruction of the function
Our knowledge of assembly
language makes this idea easier
to understand, doesnt it?

Pointers and dynamic memory


allocation

Sometimes the length of an array


is unknown at compilation time
Using pointers and the malloc()
family of instructions we can
allocate memory at run-time

Mohamed Shalan, PhD

C Quick STart

13

Arrays

Array: sequence of identical


objects in memory
int a[10]; means space for
ten integers

By itself, a is the address of the first integer


*a and a[0] mean the same thing

The address of a is not stored in memory: the compiler


inserts code to compute it when it appears
Ritchie calls this interpretation the biggest conceptual
jump from BCPL to C
Mohamed Shalan, PhD

C Quick STart

14

Multidimensional Arrays

Array declarations read right-to-left


int a[10][3][2];
an array of ten arrays of three arrays of two ints
In memory

3
...

2 2 2

2 2 2

2 2 2

10
Mohamed Shalan, PhD

C Quick STart

15

Multidimensional Arrays

Passing a multidimensional array as an argument


requires all but the first dimension
int a[10][3][2];
void examine( a[][3][2] ) { }

Address for an access such as a[i][j][k] is


a + k + 2*(j + 3*i)

Mohamed Shalan, PhD

C Quick STart

16

Function Pointer

Array of function pointers


void (*file_cmd[])(void) =
{ new_cmd,
open_cmd,
close_cmd,
save_cmd,
print_cmd,
exit_cmd
};

Function returning pointer to function

Without typedef:
int (*f(double)) (char *);
With typedef:
typedef int (*Pfunc) (char *);
Pfunc f(double);

Mohamed Shalan, PhD

C Quick STart

17

C Typedef

Type declarations recursive, complicated.


Name new types with typedef
Instead of

int (*func2)(void)
use

typedef int func2t(void);


func2t *func2;

Mohamed Shalan, PhD

C Quick STart

18

C Structures

A struct is an object with named fields:


struct {
char *name;
int x, y;
int h, w;
} box, *b;

Accessed using dot notation:


box.x = 5;
box.y = 2;

For access using pointers use ->

b->x = 5;
Dont forget to initialize b!

Mohamed Shalan, PhD

C Quick STart

19

C Unions

Can store objects of different types at different times


union {
int ival;
float fval;
char *sval;
};

Useful for arrays of dissimilar objects


Potentially very dangerous
Good example of Cs philosophy

Provide powerful mechanisms that can be abused

Mohamed Shalan, PhD

C Quick STart

20

Alignment of data in structs

Most processors require n-byte objects to be in memory at


address n*k
Side effect of wide memory busses
E.g., a 32-bit memory bus
Read from address 3 requires two accesses, shifting

4
Mohamed Shalan, PhD

C Quick STart

21

Alignment of data in structs

Compilers add padding to structs to ensure proper alignment,


especially for arrays
Pad to ensure alignment of largest object (with biggest requirement)
struct {
char a;
int b;
char c;
}

Moral: rearrange to save memory

Mohamed Shalan, PhD

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);
}

Mohamed Shalan, PhD

C Quick STart

23

Bit Manipulation (2)

a
b
c
d
e

|= 0x4; /* Set bit 2 */


&= ~0x4; /* Clear bit 2 */
&= ~(1 << 3); /* Clear bit 3 */
^= (1 << 5); /* Toggle bit 5 */
>>= 2; /* Divide e by 4 */

Mohamed Shalan, PhD

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.

Mohamed Shalan, PhD

C Quick STart

25

malloc() and free()

Library routines for managing the heap


int *a;
a = (int *) malloc(sizeof(int) * k);
a[5] = 3;
free(a);

Allocate and free arbitrary-sized chunks of


memory in any order

Mohamed Shalan, PhD

C Quick STart

26

malloc() and free()

More flexible than automatic variables (stacked)


More costly in time and space

malloc() and free() use complicated non-constant-time algorithms


Each block generally consumes two additional words of memory

Pointer to next empty block


Size of this block

Common source of errors

Using uninitialized memory


Using freed memory
Not allocating enough
Neglecting to free disused blocks (memory leaks)

Mohamed Shalan, PhD

C Quick STart

27

Dynamic Storage Allocation

What are malloc() and free() actually doing?


Pool of memory segments:

Free

malloc(

Mohamed Shalan, PhD

C Quick STart

28

The Macro Preprocessor

Relatively late and awkward addition to the


language
Symbolic constants
#define PI 3.1415926535

Macros with arguments for emulating inlining


#define min(x,y) ((x) < (y) ? (x) : (y))

Conditional compilation
#ifdef __STDC__

File inclusion for sharing of declarations


#include myheaders.h

Mohamed Shalan, PhD

C Quick STart

29

Macro Preprocessor Pitfalls

Header file dependencies usually form a directed


acyclic graph (DAG)
How do you avoid defining things twice?
Convention: surround each header (.h) file with a
conditional:
#ifndef __MYHEADER_H__
#define __MYHEADER_H__
/* Declarations */
#endif

Mohamed Shalan, PhD

C Quick STart

30

Macro Preprocessor Pitfalls

Macros with arguments do not have function


call semantics
Function Call:

Each argument evaluated once, in undefined


order, before function is called

Macro:

Each argument evaluated once every time it


appears in expansion text

Mohamed Shalan, PhD

C Quick STart

31

Macro Preprocessor pitfalls

Example: the min function


int min(int a, int b)
{ if (a < b) return a; else return b; }
#define min(a,b) ((a) < (b) ? (a) : (b))

Identical for min(5,x)


Different when evaluating expression has sideeffect:
min(a++,b)
min function increments a once
min macro may increment a twice if a < b

Mohamed Shalan, PhD

C Quick STart

32

Macro Preprocessor Pitfalls

Text substitution can expose unexpected groupings


#define mult(a,b) a*b
mult(5+3,2+4)

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:

#define mult(a,b) (a)*(b)

Mohamed Shalan, PhD

C Quick STart

33

Inline Assembly

Two reasons to add assembly into a C program:

In both cases

Need to say something that cant be said in C


Need higher performance than the C compiler provides

Write most of a function in C and then throw in a few


instructions of assembly where needed
Let the compiler do the grunt work of respecting the calling
convention

When writing asm to increase performance:

Be absolutely sure you identified the culprit


First try to write faster C

Mohamed Shalan, PhD

C Quick STart

34

Code Warrior & 68000 Example


long square (short a)
{
long result=0;
asm {

move.w a,d0 // fetch function argument a


mulu.w d0,d0 // multiply
move.l d0,result // store in local result
}
return result;

Mohamed Shalan, PhD

C Quick STart

35

GCC Inline Assembly


Format:
asm volatile (code : outputs : inputs : clobbers );

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

asm("mov %0, %1, ror #1" : "=r" (result) : "r" (value));

Mohamed Shalan, PhD

C Quick STart

36

GCC/ARM Interrupts
void __attribute__ ((interrupt("IRQ")))
tc0_cmp (void);
{
timeval++;
VICVectAddr = 0;

Other embedded compilers provide analogous


extensions
Later lectures: correct usage of interrupts

Mohamed Shalan, PhD

C Quick STart

37

C Traps & Pitfall

Mohamed Shalan, PhD

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];

Mohamed Shalan, PhD

C Quick STart

39

C Language Traps

Phantom returned values


int foo (a)
{ if (a) return(1); }

Dangling Else
if (x == 0)
if (y == 0) error();
else {
z = x + y;
f (&z);

Mohamed Shalan, PhD

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 */

Mohamed Shalan, PhD

C Quick STart

41

Language Traps

Mismatched Header Files


Suppose foo.h contains:
struct foo { BOOL a};

file F1.c contains


#define BOOL char
#include "foo.h"
file F2.c contains
#define BOOL int
#include "foo.h"

Mohamed Shalan, PhD

C Quick STart

42

Language Traps

Unsafe Returned Values


char *f() {
char result[80];
sprintf(result,"anything will do");
return(result);
}

Mohamed Shalan, PhD

C Quick STart

43

Writing Optimized C Code

Mohamed Shalan, PhD

C Quick STart

44

Optimizations Tricks

unsigned int instead of int


In processors where the * is cheaper the / use * instead of /: (a /
b) > c can be rewritten as a > (c * b)
Using arrays
switch ( queue )
{
case 0 : letter = 'W'; break;
case 1 : letter = 'S'; break;
case 2 : letter = 'U'; break;
}
Can be written as
static char *classes="WSU";
letter = classes[queue];

Mohamed Shalan, PhD

C Quick STart

45

Optimizations Tricks

Global variables

Global variables are never allocated to registers.


Hence, the compiler cannot cache the value of a
global variable in a register, resulting in extra
(often unnecessary) loads and stores when global
variables are used. We should therefore not use
global variables inside critical loops.
If a function uses global variables heavily, it is
beneficial to copy those global variables into local
variables. This is possible only if those global
variables are not used by any of the functions
which are called.

Mohamed Shalan, PhD

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

If possible, we should pass structures by reference


Pointer Chain
void InitPos1(Object *p) {
p->pos->x = 0;
p->pos->y = 0;
p->pos->z = 0;
}

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

Boolean Expressions & Range checking


(x >= min && x < max) can be transformed into
(unsigned)(x-min) < (max-min)

Use switch() instead of if...else...


If you have to use a big if..else.. statement, test the most likely
cases first.
Break things down in a binary fashion

Mohamed Shalan, PhD

C Quick STart

49

Optimizations Tricks

Use Lookup Tables

Mohamed Shalan, PhD

C Quick STart

50

Optimizations Tricks

Loop Termination

Dont use for( i=0; i<10; i++){ ...


} use for(i=10; i!=0; i--){}

Mohamed Shalan, PhD

C Quick STart

51

Optimizations Tricks

Loop Jamming

Function Looping

Mohamed Shalan, PhD

C Quick STart

52

Optimizations Tricks

Loop Unrolling

Mohamed Shalan, PhD

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.

Mohamed Shalan, PhD

C Quick STart

54

Optimizations Tricks

P4

PXA250

Mohamed Shalan, PhD

C Quick STart

55

Das könnte Ihnen auch gefallen