De presentatie wordt gedownload. Even geduld aub

De presentatie wordt gedownload. Even geduld aub

Computertechniek Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 1 C programmeren voor niet-C programmeurs les 2 definitie.

Verwante presentaties


Presentatie over: "Computertechniek Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 1 C programmeren voor niet-C programmeurs les 2 definitie."— Transcript van de presentatie:

1 Computertechniek Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 1 C programmeren voor niet-C programmeurs les 2 definitie en declaratie scope en lifetime C basis typen stdio.h constanten operatoren strings

2 Computertechniek Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 2 definitie en declaratie Een definitie: ( == volledig vastleggen ) int max( int a, int b ){ if( a > b ){ return a; } else { return b; } } Een declaratie: ( == vertellen hoe ‘t er uit ziet ) int max( int a, int b );

3 Computertechniek Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 3 definitie en declaratie Een definitie: int x; Een declaratie: extern int x;

4 Computertechniek Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 4 definitie en declaratie Conventie: Declaraties in een.h file Definities in een.c file Iedere.c file die de functies, variabelen etc. wil gebruiken doet een #include

5 Computertechniek Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 5 Scope en lifetime int n = 0; void foo( void ){ int i; for( i = 0; i < 100; i++ ){ … } n++; }

6 Computertechniek Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 6 Scope en lifetime void foo( void ){ int i; for( i = 0; i < 100; i++ ){ … } } int next( void ){ static int n =0; return ++n; }

7 Computertechniek Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 7 C basis typen integerminimaal [signed | unsigned] char+/- 127 of 0..255 [signed | unsigned] short [int]+/- 32767 of 0..65535 [signed | unsigned] int+/- 32767 of 0..65535 [signed | unsigned] long [int]+/- 2147483647 of 0..4294967295 [signed | unsigned] long long [int](64 bits) floating point float double

8 Computertechniek Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 8 C basis typen meestal is signed 2’s complement, bv -32768.. -32767 op 16-bits computers meestal 16 bit short, 16 bit int, 32 bit long op 32-bits computers meestal 16 bit short, 32 bit int, 32 bit long

9 Computertechniek Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 9 C basis typen – let op! short int = signed short int int = signed int long = signed long maar: char = signed char OF unsigned char (de compiler mag kiezen...) dus ‘char’ = 0..127 !

10 Computertechniek Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 10 C basis typen – boolean geen apart boolean type Boolean expressies: 0 is niet waar, andere getallen wel Dus let op: if( klaar )if( klaar == 1 ) if( ! klaar )if( klaar == 0 )

11 Computertechniek Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 11 Stdio.h Behalve printf() ook fprintf(), eerste argument is een file. stdin, stdout, errout open(), close() read(), write()

12 Computertechniek Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 12 constanten float pi = 3.14; void stupid( void ){ pi = 42; }

13 Computertechniek Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 13 constanten const float pi = 3.14; void stupid( void ){ pi = 42; /* hangt van de compiler af? */ }

14 Computertechniek Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 14 constanten #define pi 3.14 void stupid( void ){ pi = 42; /* hier staat: 3.14 = 42; */ }

15 Computertechniek Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 15 operatoren + - / % > = <= == != & | ^ > ~ && || ++ -- (pre en post) ? : = sizeof ik kan de prioriteiten nooit onthouden, dus ik gebruik haakjes!

16 Computertechniek Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 16 strings array of char, fixed size: char string[ 132 ]; /0 terminated, auto bij “hello” functies in string.h: strcpy, strcmp, strncmp, … let op: ’xxx’ versus ”xxx”

17 Computertechniek Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 17 Opdracht 1 ’vermenigvuldigen’ Schrijf een library (.c en.h files) met 1 functie die twee unsigned integers vermenigvuldigd tot een unsigned integer. De functie mag geen gebruik maken van *, /, % etc. De bitwise operatoren, shifts, vegelijken, en + mogen wel. Test je library functie (daarvoor mag je wel * gebruiken ).

18 Computertechniek Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 18 Binair vermenigvuldigen 0 x 0 = 0 0 x 1 = 0 1 x 0 = 0 1 x 1 = 1 Zeer eenvoudige tafel:

19 Computertechniek Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 19 Opdracht 2 ‘Brutus’ Maak een programma dat een Cesar-versleutelde text als invoer krijgt (stdin), en daaruit bepaald wat de (meest waarschijnlijke) Cesar-key was, en deze letter naar de uitvoer (stdout) schrijf. Gebruik brute force: probeer alle 27 mogelijkheden, selecteer de meest waarschijnljke op grond van (naar keuze) letterfrequentie of woordmatches.

20 Computertechniek Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 20 Werk voor volgende week Lees hoofdstuk 2 Maak (voor zover nog niet klaar) de twee opdrachten af Bewaar je opdrachten op je UsbStick


Download ppt "Computertechniek Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 1 C programmeren voor niet-C programmeurs les 2 definitie."

Verwante presentaties


Ads door Google