Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Multiple files

Reply
Thread Tools

Multiple files

 
 
fb
Guest
Posts: n/a
 
      09-02-2003
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...

 
Reply With Quote
 
 
 
 
Pieter Droogendijk
Guest
Posts: n/a
 
      09-02-2003
On Tue, 02 Sep 2003 09:53:19 GMT
fb <(E-Mail Removed)> 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#;+,)/'///*");}
 
Reply With Quote
 
 
 
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      09-02-2003
fb <(E-Mail Removed)> wrote in <jMZ4b.77063$(E-Mail Removed)>:

>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.
 
Reply With Quote
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      09-02-2003
Irrwahn Grausewitz <(E-Mail Removed)> wrote in
<(E-Mail Removed)>:
>>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.
 
Reply With Quote
 
Kevin Bracey
Guest
Posts: n/a
 
      09-02-2003
In message <jMZ4b.77063$(E-Mail Removed)>
fb <(E-Mail Removed)> 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/
 
Reply With Quote
 
Martin Dickopp
Guest
Posts: n/a
 
      09-02-2003
fb <(E-Mail Removed)> 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;
}
 
Reply With Quote
 
Martin Dickopp
Guest
Posts: n/a
 
      09-02-2003
Irrwahn Grausewitz <(E-Mail Removed)> writes:

> then put the function declaration in a separate .c file

^^^^^^^^^^^
ITYM "definition".

Martin
 
Reply With Quote
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      09-02-2003
Martin Dickopp <(E-Mail Removed)> wrote in
<bj1v4t$fc$06$(E-Mail Removed)-online.com>:

>Irrwahn Grausewitz <(E-Mail Removed)> 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.
 
Reply With Quote
 
fb
Guest
Posts: n/a
 
      09-02-2003


Martin Dickopp wrote:
> fb <(E-Mail Removed)> 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?

 
Reply With Quote
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      09-02-2003
fb <(E-Mail Removed)> wrote in <n195b.78696$(E-Mail Removed)>:
<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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
#define and #undef influence over all the files (Multiple C Files) karthikbalaguru C Programming 3 11-27-2008 01:45 PM
multiple rake build files? large rake files thufir Ruby 3 04-12-2008 07:28 AM
how i can extract text from the PDF files,power point files,Ms word files? crazyprakash Java 4 10-30-2005 10:17 AM
Text files read multiple files into single file, and then recreate the multiple files googlinggoogler@hotmail.com Python 4 02-13-2005 05:44 PM
Beginner Help: Joining Multiple classes in multiple files? JHenstay C++ 3 01-11-2004 02:28 PM



Advertisments