I am trying to learn how to use multiple modules and hearder files. I have tried a little experiment but cannot get it to work. Here is my code and compilation attempt. Any help with finding my problems appreciated.
The main function (main01.c) calls a function located in another file (flasher01.c) that adds two integers and a constant which is defined in the header file (flasher01.h) and then outputs the result.
[me@myplace]$ cat main01.c
/* main01.c
testing multi modules */
#include <stdio.h> #include "flasher01.h"
int main (void)
{
int a = 3, b=4, c;
c = adder(a,b);
printf ("%d%c", c, '\n');
}
[me@myplace]$ cat flasher01.c
/* flasher01.c
muulti module test */
#include "flasher01.h"
int adder (int int1, int int2)
{
return int1 + int2 + CONSTANT;
}
[me@myplace]$ cat flasher01.h
/* flasher01.h
testing multi modules */
#define CONSTANT 22
int adder (int int1, int int2)
[me@myplace]$ gcc main01.c flasher01.c flasher01.h -o mainApp
main01.c: In function `adder':
main01.c:11: error: parse error before '{' token
main01.c:10: error: parm types given both in parmlist and separately
flasher01.c: In function `adder':
flasher01.c:10: error: parse error before '{' token
flasher01.c:9: error: parm types given both in parmlist and separately
gcc: compilation of header file requested
[me@myplace]$
main01.c:11: error: parse error before '{' token
main01.c:10: error: parm types given both in parmlist and separately
There is no need for "void" in main. If you don't need any arguments, just make main:
main() {}
not:
main(void) {}
And to slightly improve your code:
/* main01.c
testing multi modules */
#include "flasher01.h"
int main()
{
int a = 3, b = 4;
printf ("%d\n", adder(a, b));
}
I got rid of stdio.h, because I #included it in flasher01.h:
/* flasher01.h
testing multi modules */
#include <stdio.h>
#define CONSTANT 22
int adder (int int1, int int2); /* you forgot a semi-colon */
You also do not need a, b, or c. I kept a and b, because they seemed more necessary then c. I just printed the return value of adder(), instead of putting the return value of adder into a seperate variable, and then printing it. This only saves a small amount of memory, but it still helps improve the program. If you wanted to save even more, don't even define a and b. Just make adder():
Thanks very much. One question, does main() have to have a return type?
I now have
int main()
could this just have easily been
void main()
?
I think this is what I originally intended.
Yes. void means doesn't have a return value. But you don't need int in front of main(), because functions in C (and main() is a function), are expected to return int by defualt.
There is no need to put void as function arguements, but people do it anyway, probaly for clarifacation when others read their code. But remember that function(void) isn't required, but void function() is. The latter means a function without a return value.
if the return type is empty it is considered an 'int'.
if the function has a void or a list of arguments those are the arguments that the compiler will check against.
if the argument list is empty it is effectively saying "I don't know what the arguments are" and you can call it with any arguments you like and may only get a warning. It is equivalent of ellipses, (...)