473,626 Members | 3,369 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Regarding use of modules

I am trying to split my program in different parts.

I have a file called mainfile.c that contains the main() function, some
global variables and a few other functions.

I then have a file called mainfile.h that contains some structs and a list
of the functions that are used in mainfile.c

I am then trying to make another file called my_func.c that contains some
other functions that when called will update some of the global variables
defined in mainfile.c. I guess the functions that I implement here needs to
be mentioned in mainfile.h. But what about the global variables should the
not be moved from mainfile.c to mainfile.h?
Nov 15 '05 #1
8 1368
Paminu wrote:
I am trying to split my program in different parts.

I have a file called mainfile.c that contains the main() function, some
global variables and a few other functions.

I then have a file called mainfile.h that contains some structs and a list
of the functions that are used in mainfile.c
No, mainfile.h should contain a list of the symbols *provided* by
mainfile.c for use by other units. Most likely, there are none. (And
it's very likely those structs you mention should be moved to other
header files too.)
I am then trying to make another file called my_func.c that contains some
other functions that when called will update some of the global variables
defined in mainfile.c.
This is not a good way of going about it. The whole problem with global
variables is that you lose all sight of where they are being used, and
in what way.
I guess the functions that I implement here needs to
be mentioned in mainfile.h. But what about the global variables should the
not be moved from mainfile.c to mainfile.h?


If you really wanted to use a global variable to be used across units,
it can be done, of course. Assuming you define it in mainfile.c, then it
should be declared in mainfile.h:

mainfile.h:
extern int global_var; /* Declare here */

mainfile.c:
int global_var = 0; /* Define here */

my_func.c:
#include "mainfile.h "

void foo() {
/* Use declared global_var */
}

But then in mainfile.c you have to be aware that global_var can be
modified by anything that includes mainfile.h. This is usually not what
you want. Either global_var can be passed and returned by functions:

mainfile.c:
int main(void) {
int local_var = ...;

local_var = foo(local_var); /* Pass by value */
bar(&local_var) ; /* Pass pointer ("pass by reference") */

my_func.c:
int foo(int parameter) {
/* Compute something */
return ...;
}

void bar(int* parameter) {
/* Modify directly */
*parameter = ...;
}

Or you can move the variable to the only unit it belongs to, if there is
only one:

my_func.c:
static int local_var;

void foo() {
/* Use local_var, not visible beyond my_func.c */
}

Or you can even move the variable to the only function it belongs to, if
there is only one:

void foo() {
static int local_var = 0;
/* Use local_var, is remembered across invocations of foo(), but not
visible anywhere else */
}

Using a truly global variable accessed from multiple units is a practice
that should be avoided.

Consult a good book on C; this topic will be covered.

S.
Nov 15 '05 #2
On Sun, 16 Oct 2005 21:32:16 +0200, Paminu <ja******@asd.c om> wrote:
I am trying to split my program in different parts.

I have a file called mainfile.c that contains the main() function, some
global variables and a few other functions.

I then have a file called mainfile.h that contains some structs and a list
of the functions that are used in mainfile.c
A header file should contain function prototypes and
type/variable/object declarations, not definitions.

I am then trying to make another file called my_func.c that contains some
other functions that when called will update some of the global variables
defined in mainfile.c. I guess the functions that I implement here needs to
be mentioned in mainfile.h. But what about the global variables should the
not be moved from mainfile.c to mainfile.h?


The global variables should still be defined in mainfile.c but should
be declared as external in mainfile.h. Prototypes for the new
functions should also be added to mainfile.h.
<<Remove the del for email>>
Nov 15 '05 #3
Paminu wrote:
I am trying to split my program in different parts.

I have a file called mainfile.c that contains the main() function, some
global variables and a few other functions.

I then have a file called mainfile.h that contains some structs and a list
of the functions that are used in mainfile.c

I am then trying to make another file called my_func.c that contains some
other functions that when called will update some of the global variables
defined in mainfile.c. I guess the functions that I implement here needs to
be mentioned in mainfile.h. But what about the global variables should the
not be moved from mainfile.c to mainfile.h?


What you are doing is learning rules for effective modularization.

The compiler requires that all relevant information be available when a
module is compiled. When there is information which more than one
module needs, such as declarations for functions, structures, and global
variables, a recommended way to achieve this is with a header file that
is included in multiple code files as needed.

Here is how I organize my code and header files:
A code file usually contains a function or set of related functions,
such as those needed to drive an LCD. The file might be named lcd.c and
have several functions needed to control the LCD. It may have other
functions as well that are used by the various LCD routines, such as
checking LCD status. The functions that are called by other modules
have prototypes in an associated header file, lcd.h. The functions that
are only referenced internally do not have prototypes in lcd.h. They
might have prototypes in lcd.c or I may simply define the functions
before they are referenced, so that no separate prototype is unneeded.
These internal functions are declared static to prevent access from
outside the lcd module.

The header file associated with the code file (lcd.h in this example)
contains all the declarations and information needed to use lcd.c. It
literally defines the interface for those functions. This includes a
fair amount of comments preceding each function prototype. You
shouldn't need to read the code file to know what it does.

When I have global variables (which most programmers try to minimize),
they are declared in a header file, which is included by any module
referencing. It usually is declared in header file for the module most
responsible for the variable.

I sometimes have header files not associated with code files that define
project-wide parameters or types.

These rules are my requirements, beyond what is required by the
compiler, which help me organize my code for my own understanding.

The book The Pragmatic Programmer contains a recommendation called the
DRY principle -- Don't Repeat Yourself -- which means defining something
in only one place. In this case, it is the declarations of the
functions and global variables. When they are defined in only one
place, you don't have consistency problems, which can happen if
something is defined in multiple places and then changed later,
erroneously only in some, but not all places. The principle applies to
many other areas of programming, as well, such as application-related
constants.

A major challenge in programming is managing complexity. One of the
most effective techniques is good modularization. Others, of course,
will suggest additional or replacement concerns. ;-)

Thad

Nov 15 '05 #4
Paminu a écrit :
I am trying to split my program in different parts.

I have a file called mainfile.c that contains the main() function, some
global variables and a few other functions.
The compile unit with main() is the upper level. It may contain static
(private) functions and variables, but nothing 'exportable' (apart from
main() of course)
I then have a file called mainfile.h that contains some structs and a list
of the functions that are used in mainfile.c
Sounds like bad design.
I am then trying to make another file called my_func.c that contains some
other functions that when called will update some of the global variables
defined in mainfile.c. I guess the functions that I implement here needs to
be mentioned in mainfile.h. But what about the global variables should the
not be moved from mainfile.c to mainfile.h?


Here is a typical organisation :

/* main.c */

#include "xxx.h"

struct T
{
int dummy;
};

static void do_a(void)
{
/* ... */
}

static void do_b(struct T *p)
{
/* ... */
}

/* entry point */
int main (void)
{
struct T a;
struct xxx x;

do_a();
do_b(&a);

xxx_do_c(&x);
xxx_do_d(&x);

}

/* xxx.h */
#ifndef H_XXX
#define H_XXX

struct xxx
{
int dummy;
};

void xxx_do_c(struct xxx *self);
void xxx_do_d(struct xxx *self);

#endif /* guard */

/* xxx.c */
#include "xxx.h"

/* entry points */
void xxx_do_c(struct xxx *self)
{
/* ... */
}

void xxx_do_d(struct xxx *self)
{
/* ... */
}

And please avoid globals. I most cases, you don't need them. The design
trick is to organize the code around a data structure that holds all you
need to be of permanent duration. The code stay unique, and the data can
be instancied.

Make this your design guideline and you'll write clear, modular,
unit-testable (hence reliable) and reusable code.

--
C is a sharp tool
Nov 15 '05 #5
Skarmander a écrit :
If you really wanted to use a global variable to be used across units,
it can be done, of course. Assuming you define it in mainfile.c, then it
should be declared in mainfile.h:

mainfile.h:
extern int global_var; /* Declare here */

mainfile.c:
For consistency :

#include "mainfile.h "
int global_var = 0; /* Define here */

my_func.c:
#include "mainfile.h "

void foo() {
/* Use declared global_var */
}

--
C is a sharp tool
Nov 15 '05 #6
Emmanuel Delahaye wrote:
Skarmander a écrit :
If you really wanted to use a global variable to be used across units,
it can be done, of course. Assuming you define it in mainfile.c, then
it should be declared in mainfile.h:

mainfile.h:
extern int global_var; /* Declare here */

mainfile.c:

For consistency :

#include "mainfile.h "


I was saving that for the Director's Cut.

But you're right, of course. Omitting this could lead to linker errors
when you fail to keep things synchronized, or worse, it *doesn't* lead
to linker errors... if you catch my drift.

S.
Nov 15 '05 #7
Paminu wrote:
I am trying to split my program in different parts.

I have a file called mainfile.c that contains the main() function, some
global variables and a few other functions.

I then have a file called mainfile.h that contains some structs and a list
of the functions that are used in mainfile.c

I am then trying to make another file called my_func.c that contains some
other functions that when called will update some of the global variables
defined in mainfile.c. I guess the functions that I implement here needs to
be mentioned in mainfile.h. But what about the global variables should the
not be moved from mainfile.c to mainfile.h?


Don't define objects in headers. The C header is for declaration of
information, not definitions of objects.

Assuming you program consists of two translation units, mainfile.c and
my_func.c, then create mainfile.h to be #included in both C modules. As
chief programmer, you have decided that the global variable 'int glob;'
will be defined at file scope in mainfile.c and you tell the world by
writing..

extern int glob;

...in mainfile.h.

Now because mainfile.h is included in my_func.c, glob is available
my_func and from mainfile by simply using its name.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 15 '05 #8
Thad Smith wrote:
When I have global variables (which most programmers try to minimize),
they are declared in a header file, which is included by any module
referencing.
It usually is declared in header file for the module most
responsible for the variable.


With an "extern" specifier?

--
pete
Nov 15 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
1931
by: michael | last post by:
Hello, Apologies if this seems like a trivial question, but it would help me with my python, coming from a Java background. What is the preferred naming convention for a .py file, that contains a class ? For example, if I have one class (MyClass), I wouldn't really want to put it in myclass.py, as I'd end up referencing myclass.MyClass (unless I use the import-from which I'm trying to avoid).
15
2584
by: Nick Coghlan | last post by:
Python 2.4's -m command line switch only works for modules directly on sys.path. Trying to use it with modules inside packages will fail with a "Module not found" error. This PEP aims to fix that for Python 2.5. Previously, posting of a draft version of the PEP to python-dev and python-list didn't actually generate any responses. I'm not sure if that's an indication that people don't see the restriction to top-level modules as a problem...
3
1641
by: praba kar | last post by:
Dear All, In Php we can do all the mailing operations like sending a text as a message, adding attachment to a mail, adding additional headers and so on using Mail_mime class. I want to know like that class or modules in Python. I already gone through MimeWriter,smtplib and so on. But I can't get clear details. so if anyone know regarding this kindly give me answer
4
2063
by: fh1996 | last post by:
csc /target:module MyMod.cs What's meaning of "NETMODULE"? Is /target:module always going to generate xxx.netmodule? Does C# compiler only generate the following 3 types of files: .EXE, .DLL and .NETMODULE? When should produce .NETMODULE files instead of others?
2
1115
by: msnews.microsoft.com | last post by:
Hi Every Body, I build an application using vb.net. Now i m deploying on the client system. I know that the deployment automatically check the .net framework on the client system at startup. I include the .net framework in the deployment setup. I want that if the framework is not installed on the client system then my deployment wizared automatically install the framework which i include in the deployment package. How can i do this....
1
1904
by: Ted | last post by:
I managed to get it installed OK, along side MS Visual Studio 2005 (with which I received it). During the install, I made sure I installed everything. I have developed a number of applications using MySQL v 5 and PostgreSQL, but I have not worked with MS SQL. Playing with it after installing it, and running through several tutorials, I generally like what I see, but there are a few issues (so far - I am sure others will crop up as...
1
335
by: chandrapsg | last post by:
Hi, i am working with jsp .. i wanna help regarding how to import or how to call python modules to jsp if a piece of code is availabe will be very helpful for me chandra
1
1741
by: Jim Flanagan | last post by:
Hello - I am in need of more help regarding an approach to accomplishing the following: We have a need to change the Volume serial numbers of a bunch of preprogrammed IDE Solid State Drive modules. The modules have been duplicated and thus the need to change the serial number on each one. My approach is to use a USB to IDE adapter cable and write a C# routine which will automate the writing of the serial #. At this point, I have a...
3
1009
by: Hussein B | last post by:
Hey, Is the standard library of Python is compiled (you know, the pyc thing)? Is it allowed to edit the source code of the standard library? I'm not talking about submitting the modified code to Python source code repository, I'm just asking if some one can edit the source code in his own machine. Thanks.
0
8265
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8196
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8637
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8364
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8504
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4092
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4197
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1808
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1511
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.