Go Back   The UNIX and Linux Forums > Top Forums > Programming
.
google site



Programming Post questions about C, C++, Java, SQL, and other programming languages here.

Closed Thread
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-27-2007
Registered User
 

Join Date: Mar 2007
Posts: 39
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
  #2 (permalink)  
Old 05-27-2007
Registered User
 

Join Date: Feb 2007
Posts: 67

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 (permalink)  
Old 05-27-2007
porter porter is offline Forum Advisor  
Registered User
 

Join Date: Jan 2007
Posts: 2,965
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 (permalink)  
Old 05-27-2007
Registered User
 

Join Date: Mar 2007
Posts: 39
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 (permalink)  
Old 05-28-2007
Registered User
 

Join Date: Feb 2007
Posts: 67
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 (permalink)  
Old 05-28-2007
porter porter is offline Forum Advisor  
Registered User
 

Join Date: Jan 2007
Posts: 2,965
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 (permalink)  
Old 05-28-2007
Registered User
 

Join Date: Mar 2007
Posts: 39
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 03:41 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2010. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0