473,414 Members | 1,636 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,414 software developers and data experts.

How should I create files for different functions?

5
Hi, I need some help with this question.

I am writing a main functions, with MACRO and global variables, and lots of supporting functions a(), b(), c(),........z() that are called by main function and also use the MACRO and global variables too. In order to avoid the mess to put all of them in main.c, I am thinking of creating other files for different functions.

I think I am trying to express is how to partition functions to differnt c files.
Is there any suggestion how to do that?

Thanks a lot for your help!

JJ
Sep 8 '07 #1
5 1443
Savage
1,764 Expert 1GB
Hi, I need some help with this question.

I am writing a main functions, with MACRO and global variables, and lots of supporting functions a(), b(), c(),........z() that are called by main function and also use the MACRO and global variables too. In order to avoid the mess to put all of them in main.c, I am thinking of creating other files for different functions.

I think I am trying to express is how to partition functions to different c files.
Is there any suggestion how to do that?

Thanks a lot for your help!

JJ
If I understood you well,then you will be able to this by using bunch of different header files.In a header file you store function declaration and definition and then in main you just include that file.

e.g #include "FunctionA.h"

Savage
Sep 9 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
I am writing a main functions, with MACRO and global variables,
Please don't do that. You are building a disaster model. I got to work on a systme like this one time. There were 380 programs and all of the variables were global. Every function has no arguments. Because of that, the ability to change the system went to zero since you would have to review every function in every program every time for every change.

To avoid this, programmers just added new global variables to avoid trouble by changing the existing ones.

There is no reason for a global variable:
1) local variable hides global variable of the same name
2) name conflicts. even within namespaces.
3) exposes implementation. No redesign
4) causes race conditions in multithreaded programs
5) expands ripple when value is screwed up. every function is a suspect
6) no guarantee the user will use global
7) expands program footprint
8) memory for globals may be limited
9) no guarantee for the order of creation. Only the globals in a single file are crerated in the order of declaration. The total order is indeterminate. Hence, your global may no be there when you need it. Especially if a global object needs a global variable in another file in its constructor. aka: the initialization fiasco
10) expands ripple when a recode is needed. All the code using globals has to be changed. N=Bad if there is a large installed base.
Sep 9 '07 #3
kreagan
153 100+
There is no reason for a global variable:
Weaknessforcats definately has a point. Though, I use static global variables sparingly. This forces the variable to be only accessable to the .c file which contains it. (However, I just started working ... so lets see if this opinion changes. lol. )

If your desire is not to declare a lot of variables per function, look into Structs.

If you have a lot of variables that will not change, I suggest using #defines for constants.

Use macros and GOTOs sparingly.
Sep 9 '07 #4
junjun
5
Thanks a lot for all your help.

I think this problem links to a lot of the behavior of the compiler. And I have checked a few references last night.

Basically,

the declarations of functions struct and #define stuff will be in the header file,

but the variable will use "extern ".

Does anyone knows about why we need to include .h file in the implementation .c file, cause we don't need a declaration while we already have the defintion.

Kind of just curious!

Thanks!
Sep 9 '07 #5
weaknessforcats
9,208 Expert Mod 8TB
Basically,

the declarations of functions struct and #define stuff will be in the header file,

but the variable will use "extern ".

Does anyone knows about why we need to include .h file in the implementation .c file, cause we don't need a declaration while we already have the defintion.
If you are referring to extern, then that is a declaration.

If you are referring to defining a global variable in a .c file, then you do not need the extern to that variable in that file.

I hope you read my Post #3 before you proceed. Just remember that two years from now when you need to change your design in a way that changes the global variables, you will ned to rework your entire body of code. And, of course, you cannot be multi-threaded with global variables.
Sep 10 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Tom Cat | last post by:
Is there anything wrong with using a lot of include files? On one part of my website, I have a form. The page it posts data to, includes a different file based on some of the values. The...
18
by: JKop | last post by:
Here's what I know so far: You have a C++ project. You have source files in it. When you go to compile it, first thing the preprocessor sticks the header files into each source file. So now...
10
by: John A Fotheringham | last post by:
I'm trying to write a procedure that having created a new database, will then create within that new database all the tables and procedures that go with it. In doing this I'm hitting the problem...
9
by: Charley Kyd | last post by:
I'm a newbie who needs advice about how to use external files of JavaScript code. I spent an hour this afternoon browsing through JavaScript books at the local book store. In about 15 different...
2
by: Matthew Hood | last post by:
I'm at a loss on how to create global functions for my website. Essentially, I have several different functions I use throughout my site and I want to be able to put these in 1 file and call them...
18
by: Al | last post by:
I'm still trying to do this but it never worked! In a .cpp file, I write the code, and at the beginning, I write: #ifndef MYLIST_H #define MYLIST_H ....to end: #endif What's wrong with it for...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
18
jhardman
by: jhardman | last post by:
Have you ever wanted to upload files through a form and thought, "I'd really like to use ASP, it surely has that capability, but the tutorial I used to learn ASP didn't mention how to do this."? ...
15
by: lxyone | last post by:
Using a flat file containing table names, fields, values whats the best way of creating html pages? I want control over the html pages ie 1. layout 2. what data to show 3. what controls to...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
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,...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.