Presentation is loading. Please wait.

Presentation is loading. Please wait.

Vervolg C Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 1 Onderwerpen voor vandaag functie definitions;.h files;

Similar presentations


Presentation on theme: "Vervolg C Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 1 Onderwerpen voor vandaag functie definitions;.h files;"— Presentation transcript:

1 vervolg C Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 1 Onderwerpen voor vandaag functie definitions;.h files; #ifndef Oefening hiermee

2 vervolg C Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 2 float pow2( float x ){ // function definition return x * x; // (== body) } // also declaration int main( void ){ float x; x = pow2( 5.7 ); // function call } 1: define before use

3 vervolg C Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 3 float pow2( float x ); // function declaration // (== prototype) int main( void ){ float x; x = pow2( 5.7 ); // function call } float pow2( float x ){ // function definition return x * x; // (== body) } 2: declare before use, define later

4 vervolg C Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 4 // main.c int main( void ){ float x; x = pow2( 5.7 ); } Body in separate file (1) // pow2.c float pow2( float x ){ return x * x; } Compiler error (of warning)

5 vervolg C Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 5 // main.c float pow2( float x ); // main.c int main( void ){ float x; x = pow2( 5.7 ); } Body in separate file (2) // pow2.c float pow2( float x ){ return x * x; }

6 vervolg C Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 6 // main.c float pow2( float x ); int main( void ){ float x; x = pow2( 5.7 ); } Body in separate file (3) // pow2.c double pow2( double x ){ return x * x; } Dit gaat helemaal mis! Float was niet nauwkeurig genoeg, dus

7 vervolg C Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 7 // main.c #include ”pow.h” int main( void ){ float x; x = pow2( 5.7 ); } Declaration in separate header file // pow2.c #include ”pow.h” float pow2( float x ){ return x * x; } // pow2.h float pow2( float x );

8 vervolg C Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 8 // cos.h #include ”pow2.h” float cos( float x ); repeated.h inclusion problem // sin.h #include ”pow2.h” float sin( float x ); // pow2.h float pow2( float x ); // main.c #include ”cos.h” #include ”sin.h” int main( void ){ float x; x = sin( 0.5 ) + cos( 0.5 ); }

9 vervolg C Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 9 repeated.h inclusion problem // #include ”cos.h” float pow2( float x ); float cos( float x ); // #include ”sin.h” float pow2( float x ); float sin( float x ); main.c na preprocessing: Compiler error (of warning)

10 vervolg C Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 10 Oplossing: gebruik #ifndef // pow2.h #ifndef pow2_h #define pow2_h float pow2( float x ); #endif

11 vervolg C Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 11 repeated.h inclusion met #ifndef // #include ”cos.h” #ifndef pow2_h #define pow2_h float pow2( float x ); #endif float cos( float x ); // #include ”sin.h” #ifndef pow2_h #define pow2_h float pow2( float x ); #endif float sin( float x ); // #include ”cos.h” #define pow2_h float pow2( float x ); float cos( float x ); // #include ”sin.h” float sin( float x ); // main.c #include ”cos.h” #include ”sin.h”

12 vervolg C Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 12 Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 12 Schrijf (en test) de C de functie int count( char s[], char c ); Deze functie geeft als return het aantal keren dat het char c voor komt in de string s. Schrijf 3 files: count.h, count.c, main.c (= een simpele test). Gebruik #ifndef zoals uitgelegd.


Download ppt "Vervolg C Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 1 Onderwerpen voor vandaag functie definitions;.h files;"

Similar presentations


Ads by Google