Skip to content

Example on how to divide c programm into multiple files #160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions OneProgramInMultipleFiles/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
a.out

17 changes: 17 additions & 0 deletions OneProgramInMultipleFiles/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#
#simple makefile with only explicyt rules
#

TARGET= a.out
$(TARGET): main.o function1.o function2.o
gcc -o $(TARGET) main.o function1.o function2.o

main.o: main.c
gcc -c main.c

function1.o: function1.c function1.h
gcc -c function1.c

function2.o: function2.c function2.h
gcc -c function2.c

8 changes: 8 additions & 0 deletions OneProgramInMultipleFiles/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
example how to create c program that uses more than one file

compile with command :
gcc -Wall main.c function1.c function2.c

or with command :
make

7 changes: 7 additions & 0 deletions OneProgramInMultipleFiles/function1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "function1.h"

int func1(int a, int b)
{
return 2*(2*a+b);
}

7 changes: 7 additions & 0 deletions OneProgramInMultipleFiles/function1.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef FUNCTION1_H
#define FUNCTION1_H

int func1(int a, int b);

#endif

7 changes: 7 additions & 0 deletions OneProgramInMultipleFiles/function2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "function2.h"

int func2(int a, int b)
{
return a-2-1*3*b;
}

7 changes: 7 additions & 0 deletions OneProgramInMultipleFiles/function2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef FUNCTION2_H
#define FUNCTION2_H

int func2(int a, int b);

#endif

18 changes: 18 additions & 0 deletions OneProgramInMultipleFiles/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <stdio.h>
#include "function1.h"
#include "function2.h"

int main(void)
{
int a, b;
printf("Insert two numbers: ");
if(scanf("%d %d", &a, &b)!=2)
{
fputs("Invalid input", stderr);
return 1;
}
printf("func1 result: %d\n", func1(a, b));
printf("func2 result: %d\n", func2(a, b));
return 0;
}