473,378 Members | 1,609 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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 1344
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.com> 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
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...
15
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...
3
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...
4
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...
2
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...
1
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...
1
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
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...
3
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.