1 / 22

Modularity in C

Modularity in C. Giovanni Squillero Politecnico di Torino - DAUIN giovanni.squillero@polito.it. Scope. An identifier is visible (i.e., it can be used) only within a region of program text called its scope Four kinds of scopes function file block function prototype. . Scope.

bertha
Download Presentation

Modularity in C

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Modularity in C Giovanni SquilleroPolitecnico di Torino - DAUINgiovanni.squillero@polito.it

  2. Scope • An identifier is visible (i.e., it can be used) only within a region of program text called its scope • Four kinds of scopes • function • file • block • function prototype. Advanced Programming

  3. Scope • An identifier is visible (i.e., it can be used) only within a region of program text called its scope • Four kinds of scopes • function • file • block • function prototype only labels! within a prototype Advanced Programming

  4. Scope: Example #include <stdio.h> int Glob; int myFunction(int parameter){ int lcl1; … } int myFunction2(int parameter){ int lcl2; … } Advanced Programming

  5. Scope: Example FILE #include <stdio.h> int Glob; int myFunction(int parameter){ int lcl1; … } int myFunction2(int parameter){ int lcl2; … } Advanced Programming

  6. Scope: Example BLOCK #include <stdio.h> int Glob; int myFunction(int parameter){ int lcl1; … } int myFunction2(int parameter){ int lcl2; … } Advanced Programming

  7. Storage duration • An object has a storage duration that determines its lifetime. • There are three storage durations: • static • automatic • allocated Advanced Programming

  8. Storage duration • An object has a storage duration that determines its lifetime. • There are three storage durations: • static • automatic • allocated Next lesson Advanced Programming

  9. Initialization • All objects with static storage duration are initialized (set to their initial values) before program startup • Automatic objects are not initialized by default Advanced Programming

  10. Storage classes specifiers • typedef • extern • static • auto • register Advanced Programming

  11. Storage classes specifier “syntactic convenience only” • typedef • extern • static • auto • register Advanced Programming

  12. Storage classes specifier AUTOMATIC STORAGEDURATION • typedef • extern • static • auto • register Advanced Programming

  13. Storage classes specifier STATIC STORAGEDURATION • typedef • extern • static • auto • register Advanced Programming

  14. Linkages of identifiers • External linkage • Internal linkage • No linkage Advanced Programming

  15. Linkages of identifiers ID2 EXTERNAL ID1 ID1 INTERNAL ID2 LINKER Advanced Programming

  16. Linkages: Example #include <stdio.h> int Glob;static int _Private; int myFunction(int parameter){ int lcl; extern ref; … return 0;} Advanced Programming

  17. Linkages: Example NO LINKAGE #include <stdio.h> int Glob;static int _Private; int myFunction(int parameter){ int lcl; extern ref; … return 0;} Advanced Programming

  18. Linkages: Example INTERNAL #include <stdio.h> int Glob;static int _Private; int myFunction(int parameter){ int lcl; extern ref; … return 0;} Advanced Programming

  19. Linkages: Example EXTERNAL #include <stdio.h> int Glob;static int _Private; int myFunction(int parameter){ int lcl; extern ref; … return 0;} Advanced Programming

  20. Summary • Scope • Where an identifier can be used • Storage • When an object can be used • Linkage • When an object can be used Advanced Programming

  21. What about functions? • Function identifiers • Always file scope • Always static storage duration • Either external (default) or internal (static functions) linkage Advanced Programming

More Related