Your SlideShare is downloading. ×
  • Like
15 3. modulization
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

15 3. modulization

  • 202 views
Published

 

Published in Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
202
On SlideShare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
2
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Modulization
  • 2. Developing a Large Program Developing a large Program– We can split source codes into several ~. h and ~.c files– There are many merits of several small files instead of one big file• Easy to maintain source codes• Easy to co-work with other programmers• Save time when compiling: You can just compile ~.c files which aremodified not all of source codes.2
  • 3. 3Modulization Split source code into separate files#define TWO 2typedef void VOID ;int g;VOID func1(VOID) ;VOID func2(VOID ) ;VOID main(VOID ){func1() ;func2() ;g *= TWO ;}file.cVOID func1(VOID ){func2() ;g += TWO ;}VOID func2(VOID ){func1() ;g -= TWO ;}To be writtenby friend 1To be writtenby friend 2To be writtenby me
  • 4. 4Modulization Split source code into separate files– Make ~.c files for each function– Add #define, declaration, function prototypes, extern objectsPrototype of Functions#define TWO 2typedef void VOID ;int g;VOID func1(VOID) ;VOID func2(VOID) ;VOID main(VOID){func1() ;func2() ;g *= TWO ;}#define TWO 2typedef void VOID ;extern int g;VOID func2(VOID) ;VOID func1(VOID){func2() ;g += TWO ;}#define TWO 2typedef void VOID ;extern int g;VOID func1(VOID) ;VOID func2(VOID){func1() ;g -= TWO ;}file1.c file2.c file3.c
  • 5. 5Modulization Split source code into separate files – Problem– When you want to modify #define or typedef definitions• You should modify all of source files=> Solution : Use Header file#define TWO 2typedef void VOID ;int g;VOID func1(VOID) ;VOID func2(VOID) ;VOID main(VOID){func1() ;func2() ;g *= TWO ;}#define TWO 2typedef void VOID ;extern int g;VOID func2(VOID) ;VOID func1(VOID){func2() ;g += TWO ;}#define TWO 2typedef void VOID ;extern int g;VOID func1(VOID) ;VOID func2(VOID){func1() ;g -= TWO ;}file1.c file2.c file3.c
  • 6. 6Modulization Split source code into separate files– Generally create one ~.h file for each ~.c file– Content of ~.c• Functions• Definition and Declaration used the functions in the file• #include header files necessary in the file– Content of ~.h• Extern definition of global variable accessed in the corresponding~.c file• Prototype of function defined in the corresponding ~.c file• #include header files if necessary
  • 7. 7Modulization Split source code into separate filesextern int g VOID func1(VOID) ; VOID func2(VOID) ;int g;VOID main(VOID){func1() ;func2() ;g *= TWO ;}VOID func1(VOID){func2() ;g += TWO ;}VOID func2(VOID){func1() ;g -= TWO ;}file1.h file2.h file3.hfile2.c file3.cfile1.c#define TWO 2typedef void VOID ;def.h
  • 8. Modulization Split source code into separate files– Include necessary ~.h files in ~.c and ~.h filesfile1.h file2.h file3.hextern int g#include “def.h”VOID func1(VOID) ;#include “def.h”VOID func2(VOID) ;#include “def.h”#include “file2.h”#include “file3.h”int g;VOID main(VOID){func1() ;func2() ;g *= TWO ;}#include “def.h”#include “file1.h”#include “file3.h”VOID func1(VOID){func2() ;g += TWO ;}#include “def.h”#include “file1.h”#include “file2.h”VOID func2(VOID){func1() ;g -= TWO ;}def.h#define TWO 2typedef void VOID ;file2.c file3.cfile1.c 8
  • 9. Conditional Compilation Problem with relation of header files– Some header files are included more than once• Cause overlapped definitions– Solve with conditional compilation!file1.h file2.h file2.hfile1.c file2.c file3.cdef.h#define TWO 2typedef void VOID ;9def.h is included twice“TWO” is defined twice
  • 10. Conditional Compilation Conditional compilation based on #ifndef, #endif– Place all of content of header file between #ifndef and #endif– Insert #define UNIQUE_NAME10
  • 11. 11Conditional Compilation#ifndef _FILE1_H_#define _FILE1_H_extern int g#endif#ifndef _FILE2_H_#define _FILE2_H_#include “def.h”VOID func1(VOID) ;#endif#ifndef _FILE3_H_#defien _FILE3_H_#include “def.h”VOID func2(VOID) ;#endif#include “def.h”#include “file2.h”#include “file3.h”int g;VOID main(VOID){func1() ;func2() ;g *= TWO ;}#include “def.h”#include “file1.h”#include “file3.h”VOID func1(VOID){func2() ;g += TWO ;}#include “def.h”#include “file1.h”#include “file2.h”VOID func2(VOID){func1() ;g -= TWO ;}#ifndef _DEF_H_#define _DEF_H_#define TWO 2typedef void VOID ;#endiffile2.c file3.cfile1.cfile1.h file2.h file3.hdef.h
  • 12. 12#ifndef _FILE1_H_#define _FILE1_H_extern int g#endif#ifndef _FILE2_H_#define _FILE2_H_#include “def.h”VOID func1(VOID) ;#endif#ifndef _FILE3_H_#defien _FILE3_H_#include “def.h”VOID func2(VOID) ;#endif#include “def.h”#include “file2.h”#include “file3.h”int g;VOID main(VOID){func1() ;func2() ;g *= TWO ;}#include “def.h”#include “file1.h”#include “file3.h”static int g1 ;VOID func1(VOID){func2() ;g += TWO ;}static VOID sfunc1(VOID){}#include “def.h”#include “file1.h”#include “file2.h”static int g2 ;VOID func2(VOID){func1() ;g -= TWO ;}static VOID sfunc2(VOID){}#ifndef _DEF_H_#define _DEF_H_#define TWO 2typedef void VOID ;#endiffile2.c file3.cfile1.cfile1.h file2.h file3.hdef.h