473,406 Members | 2,208 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,406 software developers and data experts.

Multiple files

fb
One quick question about multiple files.

Let's say I had the following:

#include <stdio.h>

void Heading(void);

int main(void)
{
printf("Hello. I'm about to call my Heading() function\n\n");
Heading();
printf("Now I'm back inside \"main\".\n");
return 0;
}

void Heading(void)
{
printf("Hello! You are currently inside the Heading() ");
printf("function.\n");
}

How would I split this up into seperate files? Would I need a header
file (.h) and a seperate (.c) file in addition to the main (.c) file
that has the main() function in it?

What would go where?
Thanks!

fb goes looking for his missing C book...

Nov 13 '05 #1
13 4729
On Tue, 02 Sep 2003 09:53:19 GMT
fb <me@no.net> wrote:
What would go where?
Thanks!


It could go all in the same directory:

headingheader.h:
void Heading(void);

mainfile.c:
#include <stdio.h>

#include "headingheader.h"

int main(void)
{
printf("Hello. I'm about to call my Heading() function\n\n");
Heading();
printf("Now I'm back inside \"main\".\n");
return 0;
}

headingfile.c:
#include <stdio.h>

#include "headingheader.h"

void Heading(void)
{
printf("Hello! You are currently inside the Heading() ");
printf("function.\n");
}

--
char*x(c,k,s)char*k,*s;{if(!k)return*s-36?x(0,0,s+1):s;if(s)if(*s)c=10+(c?(x(
c,k,0),x(c,k+=*s-c,s+1),*k):(x(*s,k,s+1),0));else c=10;printf(&x(~0,0,k)[c-~-
c+"1"[~c<-c]],c);}main(){x(0,"^[kXc6]dn_eaoh$%c","-34*1'.+(,03#;+,)/'///*");}
Nov 13 '05 #2
fb <me@no.net> wrote in <jM********************@news1.calgary.shaw.ca>:
One quick question about multiple files.

Let's say I had the following:

#include <stdio.h>

void Heading(void);

int main(void)
{
printf("Hello. I'm about to call my Heading() function\n\n");
Heading();
printf("Now I'm back inside \"main\".\n");
return 0;
}

void Heading(void)
{
printf("Hello! You are currently inside the Heading() ");
printf("function.\n");
}

How would I split this up into seperate files? Would I need a header
file (.h) and a seperate (.c) file in addition to the main (.c) file
that has the main() function in it? Exactly.
What would go where? You would like to put the prototype in a header file (e.g. 'heading.h'),
which you #include in 'main.c', then put the function declaration in a
separate .c file (e.g. 'heading.c'), then compile and link the two.
Thanks!

fb goes looking for his missing C book...

Good luck! :)

Irrwahn
--
Air is water with holes in it.
Nov 13 '05 #3
Irrwahn Grausewitz <ir*****@freenet.de> wrote in
<84********************************@4ax.com>:
What would go where?

You would like to put the prototype in a header file (e.g. 'heading.h'),
which you #include in 'main.c', then put the function declaration in a
separate .c file (e.g. 'heading.c'), then compile and link the two.

Reading P.D.'s reply I've noticed I forgot to mention that you should
#include "heading.h" in 'heading.c' as well...

[I have to be more careful with the 'Send' button anyway.]
--
Proofread carefully to see if you any words out.
Nov 13 '05 #4
In message <jM********************@news1.calgary.shaw.ca>
fb <me@no.net> wrote:
One quick question about multiple files.


---heading.h---

void Heading(void);

---heading.c---

#include <stdio.h>
#include "heading.h"

/* You should include your own header file to guarantee errors */
/* if prototypes don't match. */

void Heading(void)
{
printf("Hello! You are currently inside the Heading() ");
printf("function.\n");
}

---main.c---

#include <stdio.h>
#include "heading.h"

int main(void)
{
printf("Hello. I'm about to call my Heading() function\n\n");
Heading();
printf("Now I'm back inside \"main\".\n");
return 0;
}

--
Kevin Bracey, Principal Software Engineer
Tematic Ltd Tel: +44 (0) 1223 503464
182-190 Newmarket Road Fax: +44 (0) 1223 503458
Cambridge, CB5 8HE, United Kingdom WWW: http://www.tematic.com/
Nov 13 '05 #5
fb <me@no.net> writes:
One quick question about multiple files.

Let's say I had the following:

#include <stdio.h>

void Heading(void);

int main(void)
{
printf("Hello. I'm about to call my Heading() function\n\n");
Heading();
printf("Now I'm back inside \"main\".\n");
return 0;
}

void Heading(void)
{
printf("Hello! You are currently inside the Heading() ");
printf("function.\n");
}

How would I split this up into seperate files? Would I need a header file
(.h) and a seperate (.c) file in addition to the main (.c) file that has the
main() function in it?

What would go where?


Only declarations (including function prototypes) should go into header
files. If you want to define your `Heading' function in a separate file,
write a corresponding header file with the prototype, and include it both
where the function is defined and used.

Martin
---- heading.h ----

#ifndef H_HEADING
#define H_HEADING

void Heading(void);

#endif
---- heading.c ----

#include <stdio.h>
#include "heading.h"

void Heading(void)
{
printf("Hello! You are currently inside the Heading() ");
printf("function.\n");
}
---- myprog.c ----

#include <stdio.h>
#include "heading.h"

int main(void)
{
printf("Hello. I'm about to call my Heading() function\n\n");
Heading();
printf("Now I'm back inside \"main\".\n");
return 0;
}
Nov 13 '05 #6
Irrwahn Grausewitz <ir*****@freenet.de> writes:
then put the function declaration in a separate .c file

^^^^^^^^^^^
ITYM "definition". :)

Martin
Nov 13 '05 #7
Martin Dickopp <ex*************@zero-based.org> wrote in
<bj************@news.t-online.com>:
Irrwahn Grausewitz <ir*****@freenet.de> writes:
then put the function declaration in a separate .c file

^^^^^^^^^^^
ITYM "definition". :)

Oh dear, you're absolutely right!

--
Air is water with holes in it.
Nov 13 '05 #8
fb


Martin Dickopp wrote:
fb <me@no.net> writes:

One quick question about multiple files.

Let's say I had the following:

#include <stdio.h>

void Heading(void);

int main(void)
{
printf("Hello. I'm about to call my Heading() function\n\n");
Heading();
printf("Now I'm back inside \"main\".\n");
return 0;
}

void Heading(void)
{
printf("Hello! You are currently inside the Heading() ");
printf("function.\n");
}

How would I split this up into seperate files? Would I need a header file
(.h) and a seperate (.c) file in addition to the main (.c) file that has the
main() function in it?

What would go where?

Only declarations (including function prototypes) should go into header
files. If you want to define your `Heading' function in a separate file,
write a corresponding header file with the prototype, and include it both
where the function is defined and used.

Martin
---- heading.h ----

#ifndef H_HEADING
#define H_HEADING

void Heading(void);

#endif
---- heading.c ----

#include <stdio.h>
#include "heading.h"

void Heading(void)
{
printf("Hello! You are currently inside the Heading() ");
printf("function.\n");
}
---- myprog.c ----

#include <stdio.h>
#include "heading.h"

int main(void)
{
printf("Hello. I'm about to call my Heading() function\n\n");
Heading();
printf("Now I'm back inside \"main\".\n");
return 0;
}


Ok. Would it be possible to put all my prototypes and definitions into
one header file? I suppose that would be bad form though...right?

Nov 13 '05 #9
fb <me@no.net> wrote in <n1********************@news1.calgary.shaw.ca>:
<SNIP>
... Would it be possible to put all my prototypes and definitions into
one header file? I suppose that would be bad form though...right?


It's of course possible, and for a small projects probably the best
way - you don't want to end up with one header file per prototype or
so, do you? For larger projects there is a "rule" to have one header
file per module, and maybe one header file to gather all the #include
directives you need for your main (or even another) module. This way
it's even more easy to reuse a module in other projects, without
having to find out which parts of your header file you really need -
it's already well sorted.
However, for small projects, or if you are a beginner, it's better to
have all your prototypes and declarations in one place - this way you
won't get confused about where to look for, say, a specific function
prototype and you can concentrate on your task.
OTOH, if your single header file grows to big, you should divide it
in logical units and put these in separate files - even in small
projects you may end up with sth. like 'proto.h' and 'def.h', or the
like.
Anyway, it's up to you to modularize [is this english or germish?]
your project to your own preferences and needs. It is definetly worth
thinking about /before/ you actually start writing code.

After all, there is no one-for-all rule for this.

Irrwahn
--
Rain is just liquid sunshine.
Nov 13 '05 #10
On Tue, 2 Sep 2003 12:51:09 +0200, Pieter Droogendijk
<gi*@binky.homeunix.org> wrote in comp.lang.c:
On Tue, 02 Sep 2003 09:53:19 GMT
fb <me@no.net> wrote:
What would go where?
Thanks!
It could go all in the same directory:


C doesn't have directories, although many platforms that host C
compilers do. And I fail to see what directories have to do with it,
anyway. I could create the files you describe below on two different
computers on two different continents, and still make this work with a
proper path argument to the compiler.
headingheader.h:
void Heading(void);

mainfile.c:
#include <stdio.h>

#include "headingheader.h"

int main(void)
{
printf("Hello. I'm about to call my Heading() function\n\n");
Heading();
printf("Now I'm back inside \"main\".\n");
return 0;
}

headingfile.c:
#include <stdio.h>

#include "headingheader.h"

void Heading(void)
{
printf("Hello! You are currently inside the Heading() ");
printf("function.\n");
}


--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 13 '05 #11
fb <me@no.net> wrote:
[...]
Ok. Would it be possible to put all my prototypes and definitions into
one header file? I suppose that would be bad form though...right?


You shouldn't put definitions of external-linkage objects or functions
into header filse at all - the reason is that if the header file is
included in more than one .c file in the project, as is usual, then you
will have multiple external definitions which is a no-no. In general,
definitions go in .c files, and declarations go in .h files.

- Kevin.

Nov 13 '05 #12
fb <me@no.net> writes:
Ok. Would it be possible to put all my prototypes and definitions ^^^^^^^^^^^
It is certainly a bad idea to put /definitions/ in a header file. Only
/declarations/ belong there.
into one header file? I suppose that would be bad form though...right?


The best solution, in my opinion, is to have a corresponding header file
for every source file. The header file declares the public interface of
the functionality implemented in the source file, i.e. functions that are
supposed to be called from other source files, `struct's if needed, etc.

If you're delevoping a library, that might be a possible exception. The
users of your library would probably find it inconvenient to have to
include a separate header file for every single function, so it might
make sense to group similar functions together. This is what the standard
C library does: it doesn't have <printf.h>, <fprintf.h>, <fopen.h>, etc.,
but it has <stdio.h>, which declares a number of functions dealing with
a certain "topic".

Martin
Nov 13 '05 #13
fb wrote:
.... snip ...
Would it be possible to put all my prototypes and definitions into
one header file? I suppose that would be bad form though...right?


You should think of header files as defining the **published**
interface to the code and data specified in a corresponding .c
file. At the same time you can take the precaution of declaring
other entities in the .c file as static (functions and file scope
variables) to ensure they cannot be externally accessed. This
will simplify future maintenance.

You can have multiple .h files for a specific .c file, thus
allowing finer control of external visibility. However this does
not prevent name clashes.

--
Replies should be to the newsgroup
Chuck Falconer, on vacation.
Nov 13 '05 #14

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

Similar topics

6
by: x. zhang | last post by:
Hi Guys, We know that we can use <input type=file ...> to upload one file per time to the server. My question is if there are some way to upload multiple files per time to the server. (Of...
2
by: Domenico Discepola | last post by:
Hello all. Before my arrival at my current employer, our consultants physically set up our MSSQL 7 server as follows: drive c: contains the mssql engine drive d: contains the transaction log...
3
by: Arun | last post by:
Hi, I have simple question to ask. How to write multiple Binary files to the Browser using Asp.Net and Visual C#.net I have seen examples where single binary file is written to browser. ...
5
by: vj | last post by:
Hi all, I am using C++Builder-5. I want to run multiple cpp files at the same time. If I use the C++Builder for running a cpp file (i.e., I just double click the cpp file, it then opens in the...
6
by: Steven | last post by:
I have a problem with moving the backup of my database from machine to machine. The size is 17 Gig and my network keeps timing out when I try to ftp it from machine to machine. I have had the...
2
by: Sam | last post by:
Hi All, I have a solution which consists of multiple projects and each of these projects has their own app.config file. The problem is that all of my projects in the solution pull keys from the...
10
by: kimiraikkonen | last post by:
Hi, I have an app which has a listbox and when i double click an associated fileS, i want their paths to be added into listbox in my application. This code works good when i try to open a...
3
by: Gary | last post by:
Hi all, I am writing an application which will intake arguments of filenames for processing: e.g. MyApp.exe "C:\abc.txt" "C:\def.doc" - then, MyApp will process the 2 files by parsing the...
43
by: bonneylake | last post by:
Hey Everyone, Well this is my first time asking a question on here so please forgive me if i post my question in the wrong section. What i am trying to do is upload multiple files like gmail...
4
by: MoroccoIT | last post by:
Greetings - I saw somewhat similar code (pls see link below) that does mupltiple files upload. It works fine, but I wanted to populate the database with the same files that are uploaded to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...

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.