Computertechniek C programmeren voor nietC programmeurs les 2
- Slides: 20
Computertechniek C programmeren voor niet-C programmeurs les 2 • definitie en declaratie • scope en lifetime • C basis typen • stdio. h • constanten • operatoren • strings Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 1
Computertechniek 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 ); Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology
Computertechniek 3 definitie en declaratie Een definitie: int x; Een declaratie: extern int x; Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology
Computertechniek 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 <file. h> Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology
Computertechniek 5 Scope en lifetime int n = 0; void foo( void ){ int i; for( i = 0; i < 100; i++ ){ … } n++; } Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology
Computertechniek 6 Scope en lifetime void foo( void ){ int i; for( i = 0; i < 100; i++ ){ … } } int next( void ){ static int n =0; return ++n; } Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology
Computertechniek 7 C basis typen integer minimaal [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 [int] (64 bits) floating point float double Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology
Computertechniek 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 Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology
Computertechniek 9 C basis typen – let op! short int = signed int long = signed long maar: char = signed char OF unsigned char (de compiler mag kiezen. . . ) dus ‘char’ = 0. . 127 ! Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology
Computertechniek 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 ) Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology
Computertechniek 11 Stdio. h • Behalve printf() ook fprintf(), eerste argument is een file. stdin, stdout, errout • open(), close() • read(), write() Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology
Computertechniek 12 constanten float pi = 3. 14; void stupid( void ){ pi = 42; } Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology
Computertechniek 13 constanten const float pi = 3. 14; void stupid( void ){ pi = 42; /* hangt van de compiler af? */ } Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology
Computertechniek 14 constanten #define pi 3. 14 void stupid( void ){ pi = 42; /* hier staat: 3. 14 = 42; */ } Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology
Computertechniek 15 operatoren • • + - / % > < >= <= == != & | ^ << >> ~ && || ++ -- (pre en post) ? : = sizeof ik kan de prioriteiten nooit onthouden, dus ik gebruik haakjes! Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology
Computertechniek 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” Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology
Computertechniek 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 ). Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology
Computertechniek 18 Binair vermenigvuldigen Zeer eenvoudige tafel: 0 x 0=0 0 x 1=0 1 x 0=0 1 x 1=1 Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology
Computertechniek 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. Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology
Computertechniek Werk voor volgende week • Lees hoofdstuk 2 • Maak (voor zover nog niet klaar) de twee opdrachten af • Bewaar je opdrachten op je Usb. Stick Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 20
- Computerk
- Functioneel programmeren
- Wat is programmeren
- Stopregel
- Psd programmeren
- Marqueurs de relation exemples
- Allez vous en sur les places et sur les parvis
- Schéma narratif la ficelle
- Les voitures les plus rapides du monde
- Horloge de bowman
- Mots variable et invariable
- Haribo c'est beau la vie
- Les parts de les flors
- Les actionneurs et les préactionneurs
- Les constellations les plus connues
- J'aime les bouches sans venin les cœurs sans stratagème
- Mots variables et mots invariables
- Les lettres en français
- Les arguments pour tous les sujets
- Les trois obstacles et les quatre démons
- Pronoms convenable