Basic multi module problem


 
Thread Tools Search this Thread
Top Forums Programming Basic multi module problem
# 1  
Old 05-27-2007
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]$
# 2  
Old 05-27-2007
Code:
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:
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);

# 3  
Old 05-27-2007
Quote:
Originally Posted by enuenu
[me@myplace]$ cat flasher01.h
/* flasher01.h
testing multi modules */

#define CONSTANT 22
int adder (int int1, int int2)
Your are missing a trailing semicolon in the adder prototype.
# 4  
Old 05-27-2007
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.
# 5  
Old 05-28-2007
Quote:
Originally Posted by enuenu
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.
# 6  
Old 05-28-2007
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.
# 7  
Old 05-28-2007
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()
?
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

problem during perl module installation

Hi 'm getting error while installing perl mdule on linux.can any one tell me how to resolve that error? problem is: CPAN: File::Temp loaded ok (v0.22) CPAN.pm: Going to build J/JD/JDB/Win32-OLE-0.1709.tar.gz OS unsupported Warning: No success on command Warning (usually harmless):... (1 Reply)
Discussion started by: kavi.mogu
1 Replies

2. UNIX for Dummies Questions & Answers

Basic problem with pdftotext

Hi, I have used pdftotext with good results in the past, but today for some reason I keep getting the same error message. My command is as follows: And the error message is I am using Vmware player with Ubuntu server, but I don't think that is causing this issue as I have been using... (2 Replies)
Discussion started by: Joq
2 Replies

3. UNIX for Advanced & Expert Users

Problem loading cpufreq module

I'd like to install cpufreq modules on my server . I tried sudo modprobe acpi-cpufreq but got the error FATAL: Error inserting acpi_cpufreq (/lib/modules/2.6.18-238.12.1.el5xen/kernel/arch/x86_64/kernel/cpufreq/acpi-cpufreq.ko): No such device cat /proc/cpuinfo gives this ... (11 Replies)
Discussion started by: vishwamitra
11 Replies

4. UNIX for Advanced & Expert Users

Kernel module compilation problem

I have one big module 2.6.18 kernel mod.c I want to divide this to several files. The problem is to write right Makefile lib1.h lib1.c mod.c mod.c works fine normally but when I divide into several files and try to compile with this makefile obj-m := mod.o mod-objs := lib1.o ... (3 Replies)
Discussion started by: marcintom
3 Replies

5. Linux

problem with kernel module loading

Hi masters, I am new to linux and unix forum and this is my first forum. So please excuse if I am not giving sufficient information. I will give them on request. I have created a bandwidth manager module. I am using a 2.6.9 kernel and in Red Hat 3.4.3 distribution. But when i run make... (1 Reply)
Discussion started by: iamjayanth
1 Replies

6. Shell Programming and Scripting

Compatibility problem of Tk Module in different versions

Hi, I am using Tk module in perl 5.6 and it is working fine. Now when i installed the newer version 5.10.0 then getting error that Tk module not found. But i will have to work on this newer verison only to use some other modules of perl. I want to know why Tk module is not working in newer... (6 Replies)
Discussion started by: kunal_dixit
6 Replies

7. Red Hat

Problem with kernel-module-ntfs

Hi All Im trying to access the my windows XP NTFS from Redhat linux 4.0 Enterprise edition I have downloaded the respective rpm And im able to install it successfully Then i have given the following command , but got an error Here are my partitions And when i give the below... (1 Reply)
Discussion started by: balumankala
1 Replies

8. Shell Programming and Scripting

Basic problem

Hello Friends, I am learning Perl now. I have a small query. I have a directory Z with file name Z.txt. I would like to copy this file Z.txt to 3 new dir with new filenames as follows dir 1 1.txt dir 2 2.txt dir 3 3.txt I would like to then open 1.txt from dir 1 and edit the first... (0 Replies)
Discussion started by: ramesh54
0 Replies

9. Filesystems, Disks and Memory

having problem in understanding namei module

can anyone give me some idea on unix filesystem namei's algorithsm (2 Replies)
Discussion started by: kangc
2 Replies
Login or Register to Ask a Question