|
|
|
|
google site
|
|||||||
| Forums | Register | Blog | Man Pages | Forum Rules | Links | Albums | FAQ | Users | Calendar | Search | Today's Posts | Mark Forums Read |
| Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|||
|
Basic multi module problem
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]$ |
| Sponsored Links | ||
|
|
|
|||
|
Code:
main01.c:11: error: parse error before '{' token
main01.c:10: error: parm types given both in parmlist and separatelyThere is no need for "void" in main. If you don't need any arguments, just make main: Code:
main() {}not: Code:
main(void) {}And to slightly improve your code: 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: Code:
/* 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(): Code:
adder(3, 4); |
|
|||
|
Quote:
|
|
|||
|
main()
{ } is the same as int main() { } main does return an int as used directly in a call to "exit". This is the return value that a program returns to the parent program via wait/waitpid. If you are using GCC try with -Wall -Werror, it will alert you to many common errors. |
|
|||
|
Thanks again. Further to this, why is "void" used as a parameter in a function that takes no arguments, i.e.
int main (void) I see this a bit in C (I started out learning C++) , why not just use int main() ? Is main a special case, or do you always use void in this way in a function that takes no arguments? Which of the following is correct in C? myFunction(void) OR myFunction() ? |
| Sponsored Links |
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| help with extremely basic problem | tragic54 | UNIX for Dummies Questions & Answers | 4 | 04-27-2008 12:39 PM |
| AWK Multi-Line Records Numbering Problem | RacerX | Shell Programming and Scripting | 3 | 11-01-2007 10:44 AM |
| Basic Scipting problem need help for school | 3junior | Shell Programming and Scripting | 1 | 10-24-2007 11:38 AM |
| Problem in registering new netfilter target module | Rakesh Ranjan | Programming | 0 | 11-09-2005 01:43 AM |
| having problem in understanding namei module | kangc | Filesystems, Disks and Memory | 2 | 10-22-2003 11:42 AM |