473,394 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Function declarations & use of static functions

Hello everybody,

I have a question concerning function declarations. When exactly do you have
to declare functions if you want to use them? I have two functions main()
and foo(), respectively defined in main.c and foo.c. There is no declaration
of foo() inside main.c, but main() calls foo().
If I compile both files (using gcc's -c option), and then link them, main()
calls foo() without any problem. How come ? Wasn't I suppose to declare
foo() within main.c so main() could call it?

Moreover, what exactly is the use of static functions ? Why would you want
to restrict a function to a certain file ? I have read the part in the Brian
W. Kernighan and Dennis M. Ritchie book about static functions, but they
only say that it permits to hide names that the other part of the program
does not need to access.

Any help will be appreciated,
Cheers!

- Joseph
Nov 13 '05 #1
1 11505
"Jeff" <ag*******@netcourrier.com> wrote in
news:3f***********************@nan-newsreader-02.noos.net:
Hello everybody,

I have a question concerning function declarations. When exactly do you
have to declare functions if you want to use them? I have two functions
main() and foo(), respectively defined in main.c and foo.c. There is no
declaration of foo() inside main.c, but main() calls foo().
If I compile both files (using gcc's -c option), and then link them,
main() calls foo() without any problem. How come ? Wasn't I suppose to
declare foo() within main.c so main() could call it?
If foo() preceeded main() in the file you got the prototype from the
definition. E.g.

static int foo(void) /* definition and prototype in one */
{
return 0;
}

/* Since foo() is above main(), C already has foo's prototype.
*/
int main(void)
{
return foo();
}
Moreover, what exactly is the use of static functions ?
A simplistic view can be used if not too picky: "static means the static
thing has duration for the life of the program and is restricted in scope
to the nearest enclosing brace or translation unit." My words, not the
spec. So using my smushy definition, you can say that a static function
will exist for the life of the program and be visible (scoped) to only the
file that contains it. Similarly,

int foo(void)
{
static int butter;

return ++butter;
}

int main(void)
{
foo();
return 0;
}

butter will exist for the life of the program and be visible only within
foo() since foo's opening brace the nearest enclosing brace.
Why would you
want to restrict a function to a certain file ?


The question is the otherway round, why make a function visible outside a
module? Answer, because it is absolutlely essential to using the module.
Many functions to work within a module but have not externally usable
benefit.

Try to think about a module and what interfaces into it you feel are the
minimal ones you should provide to consumers of the module and make only
those functions non-static and then prototype them in the module's
accompanying header file.

Example disk.c:

static unsigned char readRegister(int regOffset)
{
unsigned char regValue;

regValue = /* some system specific stuff */;

return regValue;
}

static void writeRegister(unsigned char regValue, int regOffset)
{
/* some system specific stuff */ = regValue;
}
int dsk_readSector(void *pBuf, size_t bufSize, unsigned long sectorLba)
{
writeRegister(sectorLba, LBA_OFFSET);
writeRegister(WRITE_PIO, CMD_OFFSET);
dma_configRead(pBuf, bufSize);

return !(readRegister(STATUS_OFFSET) & ERR);
}

So here I, the consumer disk.c, care only about reading a sector, I don't
care how disk.c does this. I care only about reading. Thus, as the
implementer I hide read/writeRegister() in the file and expose only
dsk_readSector() in disk.h for external consumers. Also, as the
implementer, I keep consumers away from the disk registers by not
publishing a means to screw with them (this is good).

Does this make sense now?

--
- Mark ->
--
Nov 13 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Thomas Matthews | last post by:
Hi, I'm getting linking errors when I declare a variable in the global scope, but not inside a function. The declarations are the same (only the names have been changed...). class Book {...
5
by: Daniel Nichols | last post by:
I've noticed that in a C module (.c, .h file combination) that if you create a function's definition before it is used in other functions than a declaration is not necessary. I believe if the...
26
by: Adam Warner | last post by:
Hello all, I'm very new to C but I have a number of years of Common Lisp programming experience. I'm trying to figure out ways of translating higher order concepts such as closures into C. The...
9
by: Grumble | last post by:
Hello everyone, I've come across some strange code. Here it is, stripped down: int main(void) { int *foo; int *bar(); foo = bar(0); return 0;
11
by: Marco Loskamp | last post by:
Dear list, I'm trying to dynamically generate functions; it seems that what I really want is beyond C itself, but I'd like to be confirmed here. In the minimal example below, I'd like to...
6
by: Ravi | last post by:
Hi All: Is there any reason for declaring functions as static in a header file if that header file is going to be included in several other files? The compiler throws a warning for every such...
12
by: Steve Blinkhorn | last post by:
Does anyone know of a way of accessing and modifying variables declared static within a function from outside that function? Please no homilies on why it's bad practice: the context is very...
12
by: aaragon | last post by:
I have this scenario: several arrays for which I have their fixed values at compilation time. Now, at runtime I need to access a specific array depending on an integer but I want to avoid if and...
28
by: Why Tea | last post by:
I seem to remember that in ANSI C, all static functions should have their function prototypes listed at the beginning of the file for better consistency (or something like that). I googled and...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.