De presentatie wordt gedownload. Even geduld aub

De presentatie wordt gedownload. Even geduld aub

DU2PRES1 : C vervolg Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 1 Beginselen van C opgaves… volgende week: ARM.

Verwante presentaties


Presentatie over: "DU2PRES1 : C vervolg Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 1 Beginselen van C opgaves… volgende week: ARM."— Transcript van de presentatie:

1 DU2PRES1 : C vervolg Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 1 Beginselen van C opgaves… volgende week: ARM bordje mee!

2 DU2PRES1 : C vervolg Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 2 Commentaar Commentaar invoegen door /* blabla */ of // kommentaar tot einde regel /* */ Commentaar mag niet worden genest! Voorzie vanaf nu uw code van een header File header items –Naam + datum van het programma –naam van de programmeur –omschrijving met doel van wijzigingen Function header items –Korte beschrijving –input en output van de functie

3 DU2PRES1 : C vervolg Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 3 printf( ”1 \n” ); // printf( ”2 \n” ); /* printf( ”3 \n” ); // printf( ”4 \n” ); /* printf( ”5 \n” ); // printf( ”6 \n” ); */ printf( ”7 \n” ); // printf( ”8 \n” ); */ printf( ”9 \n” ); // printf( ”10 \n” ); Slikt de compiler dit?

4 DU2PRES1 : C vervolg Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 4 Data types char 1 byte (signed or unsigned!) short 2 bytes ( >= 2 ) int4 bytes ( >= 2 ) long4 bytes ( >= 4 ) long long 8 bytes ( >= 4 ) float 4 bytes (IEEE 754-1985) double 8 bytes (IEEE 754-1985) pointer is an address Als een berekening uit het bereik loopt is het resultaat undefined.

5 DU2PRES1 : C vervolg Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 5 Beware of the char char kan signed of unsigned zijn signed char = (ten minste) 0.. 255 unisgned char = (ten minste) -127.. + 127 char c; // elke waarden passen hier zeker in?

6 DU2PRES1 : C vervolg Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 6 Constants int ’0’120120x12 float 1.03.5e-3

7 DU2PRES1 : C vervolg Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 7 Variabelen – what’s in a name Namen: letters, _, cijfers (maar niet vooraan) Average p number_of_students Niet toegestaan 3rd_entry all$done Slechte keuze total (en in de zelfde code ook) totals  Wel een goede keuze: entry_total all_total

8 DU2PRES1 : C vervolg Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 8 Variabelen declareren int x; // 1 variabele char a, b, c; // lijstje met variabelen int x, y = 7, z =’c’; // lijstje, 2 krijgen een beginwaarde

9 DU2PRES1 : C vervolg Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 9 Operatoren : rekenkundig 2-zijdig: + - * / % 1-zijdig: - (+) Modulo geeft rest van x%y wat overblijft na deling. De berekening wordt afgekapt en dus niet afgerond. Type van het resultaat is het ’meest-omvattende’ van de twee types en int

10 DU2PRES1 : C vervolg Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 10 Operatoren : increment en decrement 1-zijdige Operatoren: ++ --  ++ en -- in 2 versies: a = x++; a = ++x;

11 DU2PRES1 : C vervolg Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 11 & bitsgewijs AND –wordt gebruikt als masker functie. –If (0x01 & port_b); test op het LSB van poort B | bitsgewijs OR –Bv voor zetten van bits in ^ bitsgewijs XOR >> shift naar rechts 0xF0>>4 wordt 0x0F (let op type!!) << shift naar links ~ 1-complement operator Operatoren : bitwise

12 DU2PRES1 : C vervolg Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 12 Operatoren: vergelijken == != > < >= <= Resultaat is 0 voor onwaar, iets anders voor waar. Wat voor anders? Dat mag de compiler zelf weten! (Vaak 1 of -1)

13 DU2PRES1 : C vervolg Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 13 Operatoren: logisch && | ! Beide argumenten worden geinterpreteerd volgens: 0 is onwaar, iets anders is waar. het resultaat is ook weer: 0 voor onwaar, iets anders voor waar.

14 DU2PRES1 : C vervolg Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 14 Bitwise <> logisch Verwar bit operatoren niet met logische operatoren. if (a && 0x04) geeft een ander resultaat dan if (a & 0x04)!  en wat dan wel (zo exact mogelijk)?

15 DU2PRES1 : C vervolg Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 15 Operatoren: nog een paar & (adres van) * (inhoud van / waar het heen wijst) A ? B : C ( A, B, C ) sizeof( )

16 DU2PRES1 : C vervolg Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 16 Operatoren: assignment operators = *= /= %= += -= <<= >>= &= ^= |=

17 DU2PRES1 : C vervolg Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 17 Variabelen float Gebroken getallen In printf met %f Wat gaat hier fout ?: #include. float term; int main(void) { term=1/3; printf(”1/3 = %f \n”,term); return 0; }

18 DU2PRES1 : C vervolg Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 18 Nu met ints Waarom is de uitkomst -2344455? #include. int term; int main(void) { term=2+2; printf(”Hello %d term \n”); return 0; }

19 DU2PRES1 : C vervolg Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 19 Statements Declaration statement (Enumeration statement) Expression statement Compound statement Selection statement Iteration statement Jump statement

20 DU2PRES1 : C vervolg Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 20 Statements Expression Statement: add: a=b+c; Closing Expression Label Declaration Statement: int a, b=10; Declaration

21 DU2PRES1 : C vervolg Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 21 Compound statement or block { ; ……… ; } Scope (Stack frame)

22 DU2PRES1 : C vervolg Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 22 Selection if (a<b) a=b; if (a<b) a=b else b=a; if (a<b) a=b else if (a<c) a=c else b=a; switch (c) { case ‘1’: a=a+24; b=b+12; break; case ‘2’: a=a+365; }

23 DU2PRES1 : C vervolg Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 23 Iterations while (a<b) c=c*a++; do c=c*a++ while (a<b); for (c=1; a<b; a++) c=c*a;

24 DU2PRES1 : C vervolg Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 24 Jumps goto indentifier ; continue ; break ; return

25 DU2PRES1 : C vervolg Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 25 Functions int max(int a, int b, int c) { int m; if( a > b ){ m = a; } else { m = b; }; return( m > c ) ? m : c; }

26 DU2PRES1 : C vervolg Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 26 Functions Aanroepen vanuit andere functie (bijvoorbeeld uit main) Equivalentie met subroutines Parameter passing Plaats zoveel mogelijk code in functies buiten main ofwel: houd main kort!! Prototype van de functie

27 DU2PRES1 : C vervolg Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 27 Functions Voordelen –hergebruik van code in andere programma’s –overzichtelijk programmeren –mogelijk om code te verdelen over diverse programmeurs

28 DU2PRES1 : C vervolg Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 28 Functie voorbeeld #include int mijnfunctie( int getal );*/prototype van functies*/ int main ( void ){ int getal_in_main; getal_in_main=10; printf(“het getal uit mijnfunctie = %d\n”, mijnfunctie(getal_in_main)); return 0; } int mijnfunctie( int getal ){ printf(“het getal uit main = %d \n”,getal++); return getal; } vraag: Wat is de uitkomst van dit programma?

29 DU2PRES1 : C vervolg Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 29 Array declaratie Een aantal geheugenplekken van het zelfde type Het aantal ligt (in C) vast bij het compileren Declaratie: type naam[ aantal ]: float cijfers[ 100 ]; char naam[ 132 ]; int schaakbord[ 8 ][ 8 ]; Gebruik: naam[ index ]: for( i=0; i<100; i++) cijfers[ i ] = 6.7;

30 DU2PRES1 : C vervolg Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 30 Array gebruik Gebruik: naam[ index ]: for( i=0; i<100; i++) cijfers[ i ] = 6.7; Het zal de compiler worst wezen als jij dom bent: cijfers[ - 1 ] = cijfers[ 100 ]; Als parameter hoeft je alleen te vermelden dat het een array is (niet de lengte): int length( char regel[] ){ … }

31 DU2PRES1 : C vervolg Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 31 C arrays, C pointers een char array is een verzameling (0..n) characters behalve bij het definieren wordt de lengte *niet* genoemd! een array wordt geindexeerd: a[ i ] een int pointer wijst naar een verzameling (0..n) ints naar hoeveel wordt nergens genoemd! een pointer wordt gedereferenced: *a met een pointer kan je rekenen, de ‘teleenheid’ is de omvang van het basistype (bv int)

32 DU2PRES1 : C vervolg Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 32 C arrays, C pointers gebruik van een array char a[ 10 ]; for( i = 0; i < 10; i++ ){ a[ i ] = ’x’; } gebruik van een pointer char *p =...; for( i = 0; i < 10; i++ ){ *p = ’x’; p++; }

33 DU2PRES1 : C vervolg Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 33 C pointers een pointer wijst uitzichzelf (nog) nergens naar int *p; *p = 15; /* geen idee waar de pointer heen wijst! */ een pointer kan wijzen naar een ‘gewone’ variabele int i; int *p = &i; *p = 15; /* OK */ p++; int *p = 15; /* fout! */

34 DU2PRES1 : C vervolg Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 34 een array is een pointer? int a[ 100 ]; int *p; p = &a[ 0 ]; p = a; a = p; a[ 10 ] = 12; *( p + 10 ) = 12; *( a + 10 ) = 10; p[ 10 ] = 10; 1 regel is fout, de rest is goed!

35 DU2PRES1 : C vervolg Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 35 drie mogelijke implementaties van strlen a) int strlen( char s[] ){ int i = 0; while (s[i] != ‘\0’) i++; return i; } b) int strlen( char *s ){ int i = 0; while( *( s + i ) != ‘\0’){ i++ } return i; } c) int strlen( char *s ){ int i = 0; while( *s != ‘\0’){ s++; i++ } return i; }

36 DU2PRES1 : C vervolg Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 36 Opdracht: ’strncpy’ Schrijf een implementatie van de string functie char *strncpy( char *dest, char *src, size_t n ); op de a) (=array) en c) (=pointer) manieren. strncpy copieert zo veel mogelijk characters van string src naar string dest, maar ten hoogste n bytes. Na afloop bevat dest altijd een afsluitende \0.


Download ppt "DU2PRES1 : C vervolg Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 1 Beginselen van C opgaves… volgende week: ARM."

Verwante presentaties


Ads door Google