I have this C program with multiple files and I want to introduce a new file called enemy.c.
and I'm wondering if I'm going about this right.

game.h

#ifndef GAME_
#define GAME_

extern void game (void);
extern void enemyFunc (void);
extern int enemyPosX;  

#endif

main.c

int main (void) 
{
    game();
}

game.c

#include "game.h"

int enemyPosX = 452;

void game (void)
{
   enemyFunc();
}

enemy.c

#include "game.h"

void enemyFunc (void)  
{
  enemyPosX -= 4;  
}

The program works but I'm wondering if I should make a new file called enemy.h
and put

extern void enemyFunc (void);
in it instead of in game.h, to have a header defining each .c file?

>The program works but I'm wondering if I should make a new file called enemy.h
You can if you'd like. I don't see any benefit for it at this point though.

If I were to do it, I would need to include "game.h" in enemy.c as well seeing as I'm using the variable enemyPosX from it?

>If I were to do it, I would need to include "game.h" in enemy.c
>as well seeing as I'm using the variable enemyPosX from it?
I would move everything that pertains to enemies into enemy.h. Then you would need to include enemy.h in game.c, which is a smidge more intuitive.

Hi I wrote a code that may serve a good example for future visitors. Thought I'll post it anyway even though ur question has been answered.

/* Working with Multiple C files Example program by virgoptrex (c) 2010 */
/* All extern variables and structures defined here */
/*This is lol.h */

#ifndef _LOL
#define _LOL

extern int howdie;
#define me 10
extern void lol2();
extern void lol3();
extern int a1;
extern int b1;


extern int hello();

typedef struct lol123{

int dude;
char alpha;

}lol123;


extern lol123 lolol;

#endif
/* Working with Multiple C files Example program by virgoptrex (c) 2010 */
/* always a good practice to make a common group file*/
/* There may be multiple common group files in a project*/
/* This file is common to 'lol1.c','lol2.c','lol3.c'*/
/*This is common.c */

#include "lol.h"

int hello();
void lol2();
void lol3();
lol123 lolol;

int a1;
int b1;

int hello(){
int howdie=10;
return howdie;
}
/* Working with Multiple C files Example program by virgoptrex (c) 2010 */
/*This is lol1.c */

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


int main()
{
printf ("lol1:hello+me %d\n", hello()+me);
lol2();
lol3();
return 0;
}
/* Working with Multiple C files Example program by virgoptrex (c) 2010 */
/*This is lol2.c */

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

/* please don't write something like 'b1=20' here outside void lol2() function */

void lol2()
{
b1=20;
lolol.alpha='a';
lolol.dude =12;
printf ("This is lol2: %d\n", hello());
printf ("lol2: alpha = %c and dude+b1 = %d\n", lolol.alpha, lolol.dude+b1 );
}
/* Working with Multiple C files Example program by virgoptrex (c) 2010 */
/*This is lol3.c */

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



void lol3()
{
lolol.alpha='b';
lolol.dude =99;
a1=2;
printf ("hello from lol3 %d\n", hello()+me+me);
printf("lol3: alpha %c and dude+a1 %d \n", lolol.alpha, lolol.dude+a1 );

}
#The Makefile (c) 2010 by Virgoptrex
sample:
	gcc -c lol1.c lol2.c lol3.c common.c
	gcc lol1.o lol2.o lol3.o common.o -o lol

clean:
	rm -r lol1.o lol2.o lol3.o common.o lol
This question has already been answered. Start a new discussion instead.