C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 29th, 2012, 10:01 PM
sam6982 sam6982 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 5 sam6982 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 31 m 29 sec
Reputation Power: 0
Modular Code

I feel like I have a good understanding of C, but I have a problem that's been bugging me for a while, and I can't find anything on Google. I'd like to create modular code with interchangeable files, but I have no clue where to start. I'm just aiming for this program to work on Windows 7. I don't really need a detailed explanation, just some idea for an amateur programmer on how to go about doing this. Thanks in advance for any help!

Reply With Quote
  #2  
Old November 29th, 2012, 10:29 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,458 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 4 Days 6 h 26 m 43 sec
Reputation Power: 403
Others already wrote a lot of modules. Become very good at finding them and understanding how to use and connect them.

For instance, study GLib.

Write a program solving this recent thread using the GLib linked list structure.

Interested in graphics? Write a short program that displays a picture given in many formats.
__________________
[code]Code tags[/code] are essential for python code!

Reply With Quote
  #3  
Old November 29th, 2012, 10:39 PM
sam6982 sam6982 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 5 sam6982 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 31 m 29 sec
Reputation Power: 0
I understand C, but I need this to be written a little more dumbed down for me. I would just like to have separate modules of code that can be interchangeable when one is needed.

Reply With Quote
  #4  
Old November 29th, 2012, 11:12 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,254 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 5 Days 20 h 9 m 35 sec
Reputation Power: 1985
It's not really a hard, tried-and-true science, but more a kind of art form. But that doesn't mean that it's hard to do.

Do you also have a basic familiarity with C++ and object-oriented programming (OOP)? None of the fancy stuff like inheritance and virtual methods. Just the basic idea of defining data and the functions that use them, with some data and functions being public for all to access and some being private accessible only by that object. And the idea of defining objects that perform specific jobs and that other objects communicate with them through public method calls. And one goal of OOP is to create classes (the declaration of an object) that you can reuse.

If none of that made any sense to you, don't worry. But at least now you have some idea what to look forward to -- don't worry, it's exciting! My point is that one approach to your problem is to basically design each module (ie, each separate .c source file) as if it were a C++ object. For that matter, it's common in C++ to have a separate .cpp source file for each class, so practically no difference.

To begin with, by "module", we are talking about a separate .c source file.

There's the concept of namespace, which actually exists in C++ now, but not in C. Your entire C program has one big happy namespace, which means that all global functions and variables (ie, all "file scope" entities) are accessible from any other module in your program. Please note that you still need to inform the other modules of those globals with header files.

The first namespace issue is that if you create two functions with the same name in different modules, then the linker won't know which one you're trying to call and will fail. That's called a "name collision" or a "namespace collision" and it's the reason why the new C++ standard lets you create different namespaces. C has no feature to support that, so you need to implement it yourself. We do that by prefacing each function in a module with something that identifies it as being in that module; an additional advantage of this is that in a large project you know which file to go to for that function's code. For example, if the module handles serial I/O, then functions start with SIO_ ; that way the only functions that start with SIO_ will be in the sio.c module. Of course, this requires discipline on your part.

Now, there will be functions and variables that you want to keep private to its module; you don't want them to go into that global namespace. It's simple to do that; you simply declare them as static and they can only be accessed within the module. You can even create static variables and functions by the same name in all the modules and they will never collide with each other.

Now, write your modules so that each does its own thing. Write a module to handle serial I/O and you should be able to export it to all your other programs that need to use serial I/O.

You know, it might help you to read up on basic OOP for these concepts of encapsulation and organizing a program based on objects instead of mushed-together mixed-up code. And keep what I've told you in mind as to how to implement the basic OOP concepts that you're learning.

Reply With Quote
  #5  
Old November 30th, 2012, 08:19 AM
sam6982 sam6982 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 5 sam6982 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 31 m 29 sec
Reputation Power: 0
Great help!

Thanks for all the great help! This is exactly what I was looking for.

Reply With Quote
  #6  
Old November 30th, 2012, 01:35 PM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 4,824 clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 21 h 1 m
Reputation Power: 1800
The term "modular" means different things to different people, and the mechanisms for modularisation differ between languages. In C those mechanism are the function and the translation unit(using separate compilation and linking). OOP languages such as C++ add classes, inheritance and generic programming (templates) as additional mechanisms - so intrinsically OOP provides greater flexibility for modularisation.

Either way, it is a software design issue rather than a language issue, and in any case your aim is to minimise coupling and maximize cohesion. Google this two terms to get an idea of the principles of modular development. Start with these:

http://en.wikipedia.org/wiki/Modular_programming

http://en.wikipedia.org/wiki/Coupling_(computer_programming)

http://en.wikipedia.org/wiki/Cohesion_(computer_science)
Comments on this post
dwise1_aol agrees: As expressed in Msg #7.

Last edited by clifford : November 30th, 2012 at 01:41 PM.

Reply With Quote
  #7  
Old November 30th, 2012, 08:15 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,254 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 5 Days 20 h 9 m 35 sec
Reputation Power: 1985
Clifford, adding to your reputation did not allow me enough characters to speak.

It is often said that half the battle is in knowing that it can be done. When researching for information, half the battle is knowing what terminology to use. Professionals know what techniques to use, but may not always know what terminology that academics. Most entries in Google are based on academics' terminology.

I knew the ideas that the OP was looking for. Clifford knew the terminology to enable the OP to learn so much more.

Reply With Quote
  #8  
Old December 1st, 2012, 03:22 AM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 4,824 clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 21 h 1 m
Reputation Power: 1800
If you are simply asking about the structural/syntactical mechanics of modular development in C then consider the following file set for a simple program comprising of three modules (main, module1 and module2):

module1.h:
Code:
#if !defined MODULE1_INC
#define MODULE1_INC

// Module1 Declarations
void module1_function1( void ) ;
void module1_function1( void ) ;

#endif


module1.c:
Code:
#include "module1.h"

// Module1 Definitions
void module1_function1( void )
{
    //...
}

void module1_function2( void )
{
    //...
}


module2.h:
Code:
#if !defined MODULE1_INC
#define MODULE2_INC

// Module1 Declarations
void module2_function1( void ) ;

#endif


module2.c:
Code:
#include "module2.h"

// Module2 Definitions
void module2_function1( void )
{
    //...
}


main.c:
Code:
#include "module1.h"
#include "module2.h"

// Program from modules 1 and 2
int main( void )
{
    module1_function2() ;
    module2_function1() ;
    module1_function1() ;
}


Each of main.c, module1.c and module2.c must be separately compiled and linked to form a complete executable. If you are using some sort of IDE that can manage the process of separate compilation and linking for you my adding teh modules to the managed project. If you are using a command-line tool-chain, then large applications quickly become impractical to compile separately by hand and you would use some sort of script or more efficiently a makefile or similar build management tool.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Modular Code

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap