467,146 Members | 1,008 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,146 developers. It's quick & easy.

Callling a function from another file...

Hi,
I have a C file(add.c) in which i have a function called add.now
i want to call the same add function from another file sub.c .Can any1
tell how to do that...

Thanks in advance
Mark

Sep 24 '07 #1
  • viewed: 28998
Share:
17 Replies
On Sep 24, 11:01 pm, sagar <mvksa...@gmail.comwrote:
Hi,
I have a C file(add.c) in which i have a function called add.now
i want to call the same add function from another file sub.c .Can any1
tell how to do that...
Please don't use silly txt-speak here.

Make sure add has external linkage, put a prototype for it at the
start of sub.c and away to go.
>
Thanks in advance
Mark
Sep 24 '07 #2
On 24 Sep 2007 at 22:01, sagar wrote:
Hi,
I have a C file(add.c) in which i have a function called add.now
i want to call the same add function from another file sub.c .Can any1
tell how to do that...
I would do this:

/* add.c */

add(a, b)
int a; int b;
{
return (a+b);
}
/* sub.c */

sub(a, b)
int a; int b;
{
int add();
b=~b;
return ( add(a, ++b) );
}

Sep 24 '07 #3
On Sep 24, 5:01 pm, sagar <mvksa...@gmail.comwrote:
Hi,
I have a C file(add.c) in which i have a function called add.now
i want to call the same add function from another file sub.c .Can any1
tell how to do that...

Thanks in advance
Mark
The canonical way to do this is to create a header file that contains
a declaration for the function in prototype syntax, and #include it in
both the file that defines the function (add.c) and the file that
calls it (sub.c).

/*
* add.h
*/
#ifndef ADD_H
#define ADD_H

extern int add(int a, int b); /* I'm assuming that add takes ints */
/* and returns an int */

#endif

/*
* add.c
*/
#include "add.h"

int add(int a, int b)
{
/* do something interesting and return a value */
}

/*
* sub.c
*/
#include "add.h"

int sub(int a, int b)
{
int x;
...
x = add(a,b);
...
return x;
}

Now, you need to compile and link in such a way that both files are
built into the same executable. I don't know what environment you are
using, but here's a quick example using gcc:

gcc -o test sub.c add.c

Sep 24 '07 #4
Fr************@googlemail.com writes:
On Sep 24, 11:01 pm, sagar <mvksa...@gmail.comwrote:
> I have a C file(add.c) in which i have a function called add.now
i want to call the same add function from another file sub.c .Can any1
tell how to do that...

Please don't use silly txt-speak here.

Make sure add has external linkage, put a prototype for it at the
start of sub.c and away to go.
Better: put the prototype in add.h, and add '#include "add.h"' to both
add.c and sub.c.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 24 '07 #5
Fr************@googlemail.com wrote:
sagar <mvksa...@gmail.comwrote:
>I have a C file(add.c) in which i have a function called add.now
i want to call the same add function from another file sub.c .Can
any1 tell how to do that...

Please don't use silly txt-speak here.

Make sure add has external linkage, put a prototype for it at the
start of sub.c and away to go.
Surely the OP meant to call "add", not "add.now". The way to
handle this is to remove any static in add.c describing the
function add, make a header file add.h which has the prototype for
add (and include it in add.c). Also include it in sub.c. All
done.

But don't start generating loose prototypes with no connection to
anything.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Sep 25 '07 #6
On Tue, 25 Sep 2007 01:22:48 +0200 (CEST), Platonic Solid
<no****@nospam.comwrote in comp.lang.c:
On 24 Sep 2007 at 22:01, sagar wrote:
Hi,
I have a C file(add.c) in which i have a function called add.now
i want to call the same add function from another file sub.c .Can any1
tell how to do that...

I would do this:

/* add.c */

add(a, b)
int a; int b;
If you would do this, you would never get a job as a C programmer in a
legitimate shop.

[snip]

I don't think anybody in this group will take someone seriously who
hasn't noticed that this style of function definition became obsolete
in C. EIGHTEEN YEARS AGO.

If you know better and you are trolling, just say so and I will plonk
you.

If you don't know better, buy a modern book on C, such as K&R 2 from
1989. Read and understand it before you post here again.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Sep 25 '07 #7
CBFalconer <cb********@yahoo.comwrites:
Fr************@googlemail.com wrote:
>sagar <mvksa...@gmail.comwrote:
>>I have a C file(add.c) in which i have a function called add.now
i want to call the same add function from another file sub.c .Can
any1 tell how to do that...

Please don't use silly txt-speak here.

Make sure add has external linkage, put a prototype for it at the
start of sub.c and away to go.

Surely the OP meant to call "add", not "add.now".
[...]

The OP simply neglected to add a space or two after the period, and to
capitalize the first word of the next sentence.

Translated to standard English:

I have a C file, add.c, in which I have a function called add.
Now I want to call the same add function from another file, sub.c.
Can anyone tell how to do that?

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 25 '07 #8
On 25 Sep 2007 at 2:51, Jack Klein wrote:
On Tue, 25 Sep 2007 01:22:48 +0200 (CEST), Platonic Solid
<no****@nospam.comwrote in comp.lang.c:
>On 24 Sep 2007 at 22:01, sagar wrote:
Hi,
I have a C file(add.c) in which i have a function called add.now
i want to call the same add function from another file sub.c .Can any1
tell how to do that...

I would do this:

/* add.c */

add(a, b)
int a; int b;

If you would do this, you would never get a job as a C programmer in a
legitimate shop.

[snip]

I don't think anybody in this group will take someone seriously who
hasn't noticed that this style of function definition became obsolete
in C. EIGHTEEN YEARS AGO.

If you know better and you are trolling, just say so and I will plonk
you.

If you don't know better, buy a modern book on C, such as K&R 2 from
1989. Read and understand it before you post here again.
The situation is this: I am a hobbyist programmer and I've recently been
learning C from some old lecture notes. I've been told that some of the
syntax they use is now deprecated, but since all compilers will happily
compile the syntax I know, I don't really think there's any need to
start all over from the beginning. Especially as you can mix and match
old-style and new-style in the same program without any difficulty.

I also remember reading that this group caters for C according to any
standard version, including the "standard before the standard".

Sep 25 '07 #9
Platonic Solid said:

<snip>

[K&R-style function declarators]
The situation is this: I am a hobbyist programmer and I've recently been
learning C from some old lecture notes. I've been told that some of the
syntax they use is now deprecated, but since all compilers will happily
compile the syntax I know, I don't really think there's any need to
start all over from the beginning. Especially as you can mix and match
old-style and new-style in the same program without any difficulty.

I also remember reading that this group caters for C according to any
standard version, including the "standard before the standard".
In a way, you are correct. K&R-style function declarators are certainly
topical, and in fact they remain legal C even in C99 (except for the
implicit int part, but even that is legal C90).

It is certainly a good idea to be aware of K&R-style declarators. Using
them in new code, however, is a less good idea. Just because a construct
is legal C (or even legal legacy C) and therefore topical in clc, that
doesn't automatically make it a bright thing to do!

Whilst I sympathise with your reluctance to obsolesce a construct in which
you have invested learning time, there are occasions where it's best to
bite the bullet and learn the "new"[1] way of doing things, and this is
certainly one of those occasions. Fortunately, prototype-style function
declarations are *easier* to learn than K&R style, they are quicker to
type, they are easier to maintain, and they have the added advantage of
giving the compiler more scope for type-checking. It's a win-win-win-win
situation.

[1] After 18 years, I think the irony quotes are justified. :-)

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 25 '07 #10
On Tue, 25 Sep 2007 13:02:04 +0200 (CEST), Platonic Solid
<no****@nospam.comwrote in comp.lang.c:
On 25 Sep 2007 at 2:51, Jack Klein wrote:
On Tue, 25 Sep 2007 01:22:48 +0200 (CEST), Platonic Solid
<no****@nospam.comwrote in comp.lang.c:
On 24 Sep 2007 at 22:01, sagar wrote:
Hi,
I have a C file(add.c) in which i have a function called add.now
i want to call the same add function from another file sub.c .Can any1
tell how to do that...

I would do this:

/* add.c */

add(a, b)
int a; int b;
If you would do this, you would never get a job as a C programmer in a
legitimate shop.

[snip]

I don't think anybody in this group will take someone seriously who
hasn't noticed that this style of function definition became obsolete
in C. EIGHTEEN YEARS AGO.

If you know better and you are trolling, just say so and I will plonk
you.

If you don't know better, buy a modern book on C, such as K&R 2 from
1989. Read and understand it before you post here again.

The situation is this: I am a hobbyist programmer and I've recently been
learning C from some old lecture notes. I've been told that some of the
syntax they use is now deprecated, but since all compilers will happily
compile the syntax I know, I don't really think there's any need to
start all over from the beginning. Especially as you can mix and match
old-style and new-style in the same program without any difficulty.
That is possible, but a very bad idea.

There is absolutely no question that adding prototypes to the language
is the single most significant improvement in the history of the C
language.

While there are still, perhaps, a few old UNIX kernels that can only
be compiled with antique, pre-ANSI compilers, it would be almost
impossible today to find a current compiler that could not accept at
least ANSI 1989/ISO 1990 C.

Writing new code using non-prototype function definitions is at best
foolish, at worst malicious. But submitting code samples to newbies
in this group using non-prototype function definitions, or
recommending that as a coding practice to newbies, is intolerable and
will only serve to get you pummeled repeatedly.
I also remember reading that this group caters for C according to any
standard version, including the "standard before the standard".
Yes, indeed, if someone has to deal with such code, for example to
update an ancient legacy application or deal with a very old third
party library. But I wouldn't say that this group supports anyone
writing per-standard C.

There are a number of more modern tutorials available online, some are
listed in the answer to question 18.9 of this newsgroup's FAQ (link in
my signature). Of these, Tom Torf's tutorial and Steve Summit's class
notes at eskimo.com are well regarded.

Come join us in the 21st century.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Sep 25 '07 #11
Jack Klein <ja*******@spamcop.netwrites:
[...]
There is absolutely no question that adding prototypes to the language
is the single most significant improvement in the history of the C
language.
[...]

Even more significant than allowing trailing commas in enumeration
type declarations?

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 25 '07 #12
On Sep 25, 11:13 pm, Richard Heathfield <r...@see.sig.invalidwrote:
Platonic Solid said:
Especially as you can mix and match
old-style and new-style in the same program without any difficulty.
You have to define the function the same way you declared it
though; e.g. you can't have a prototype declaration and a
K&R style definition. (The compiler is not required to
diagnose this either, you just get weird runtime behaviour).
Fortunately, prototype-style function
declarations are [...] quicker to type,
Not if the K&R style was:
int func( a, b, c, d )
volatile unsigned char *a, *b, *c, *d;
{ .... }

(In fact, this argument was used by my C tutor at polytech
way back when, as to why he preferred K&R style).

Sep 25 '07 #13
On Sep 26, 4:17 am, Jack Klein <jackkl...@spamcop.netwrote:
There is absolutely no question that adding prototypes to the language
is the single most significant improvement in the history of the C
language.
Every time my compiler catches an error due to the
function call not matching the prototype, I reflect
for a moment on how much time I would have wasted
debugging if that error had not been reported.

Sep 25 '07 #14
Old Wolf <ol*****@inspire.net.nzwrites:
On Sep 26, 4:17 am, Jack Klein <jackkl...@spamcop.netwrote:
>There is absolutely no question that adding prototypes to the language
is the single most significant improvement in the history of the C
language.

Every time my compiler catches an error due to the
function call not matching the prototype, I reflect
for a moment on how much time I would have wasted
debugging if that error had not been reported.
Indeed. One of the very first C programs I wrote had an error in it:
the number of arguments in a call didn't match the number of
parameters in the declaration. The compiler didn't make a peep; it
just compiled it and generated bad code. I was *horrified*.

Prototypes are good.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 26 '07 #15
Old Wolf said:
On Sep 25, 11:13 pm, Richard Heathfield <r...@see.sig.invalidwrote:
<snip>
>Fortunately, prototype-style function
declarations are [...] quicker to type,

Not if the K&R style was:
int func( a, b, c, d )
volatile unsigned char *a, *b, *c, *d;
I just tried this at home. K&R-style: 18.28 seconds. Modern style: 22.97
seconds. So yes, if you try hard, you can find counter-examples.
Similarly, if you pit a man against a horse in a foot-race, the man can
win if the race is sufficiently short (presumably because the horse has
more legs to sort out). The existence of counter-examples demonstrates not
that the proposition is universally false, but that it is not universally
true. I accept this.

{ .... }

(In fact, this argument was used by my C tutor at polytech
way back when, as to why he preferred K&R style).
In general, modern style is faster, especially when you choose descriptive
parameter names (upon which your C tutor should have been insisting). So
it's a poor argument.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 26 '07 #16
On Mon, 24 Sep 2007 15:01:13 -0700, sagar <mv******@gmail.comwrote:
>Hi,
I have a C file(add.c) in which i have a function called add.now
i want to call the same add function from another file sub.c .Can any1
tell how to do that...
add.now is not a legal name for a function but I assume you know how
to fix that. In addition to the information you have received about
how to deal with this issue in the source modules, you must also deal
with the implementation specific issue of how you tell your linker to
include the object module of the called function in the executable you
are building from sub.c.
Remove del for email
Sep 26 '07 #17
On Tue, 25 Sep 2007 16:42:39 -0700, Old Wolf <ol*****@inspire.net.nz>
wrote:
On Sep 25, 11:13 pm, Richard Heathfield <r...@see.sig.invalidwrote:
Platonic Solid said:
Especially as you can mix and match
old-style and new-style in the same program without any difficulty.

You have to define the function the same way you declared it
though; e.g. you can't have a prototype declaration and a
K&R style definition. (The compiler is not required to
diagnose this either, you just get weird runtime behaviour).
Not quite. You can 'cross' these if (and only if) the prototype form
matches _the result of applying default argument promotions to_ the
nonprototype form. This is tricky and system-dependent enough that the
PP is unlikely to get it right, and the people who know enough to get
it right are unlikely to want to do it. About the only case I would
approve of would be where you have to interface to legacy code that
you literally can't change _at all_ not even 'syntax cleanup'. There
is never a technical need for this, but there can be legal ones.

And yes if you do get it wrong it's UB. Although in a number of simple
cases it accidentally works, because it is fairly common for calling
conventions to promote everything to at least a (machine dependent)
'word' and this often just happens to do the same thing the default
promotions do. The Standard default promotions, of course, are partly
based on what the PDP-11 convention actually did.

- formerly david.thompson1 || achar(64) || worldnet.att.net
Oct 8 '07 #18

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by bienwell | last post: by
28 posts views Thread by Bill | last post: by
15 posts views Thread by Mikhail Kovalev | last post: by
16 posts views Thread by Xiaoxiao | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.