473,761 Members | 6,993 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is there a real need to use keyword static with functions?

Hi everyone

Is there a real need to use keyword static with functions, if we
simply don't declare their prototypes in .h file?

Many textbooks avoid to discuss this matter and/or discuss only the
usage of static functions. I've been programming for years and I never
felt a real need for the "static approach for functions".

I don't know if this is a trivial matter since the reserved word
exists. :)
Or it exists for some other purpose I can't figure out.

What did I have missed? :)

Thanks in advance for answers

Mar 5 '07
32 3052
In article <11************ **********@j27g 2000cwj.googleg roups.com>,
<ma**********@p obox.comwrote:
>On 6 Mar, 14:35, lcdgoncal...@gm ail.com wrote:
>On 5 mar, 18:41, "user923005 " <dcor...@connx. comwrote:

The author of that book wouldn't have been Herbert Schildt, would it?

Well... it's time to study a little more. I have a copy of HS's book
somewhere. :) Perhaps I've missed something the first time I read it.

The recommendation from the group would probably be to ditch HS's book
- he's not regarded in great esteem round here...
"Ditch" is a bit weak. Try "Burn it and post videos".
dave

--
Dave Vandervies dj******@csclub .uwaterloo.ca
It was worthwhile to complain to you, since I know you are bright enough to
understand the point. (Foul-mouthed non-regs simply get plonked without
notice; it's a cost-benefit trade-off!) --Richard Heathfield in comp.lang.c
Mar 6 '07 #21
On Mar 5, 9:41 pm, "user923005 " <dcor...@connx. comwrote:
On Mar 5, 1:10 pm, lcdgoncal...@gm ail.com wrote:
Hi everyone
Is there a real need to use keyword static with functions, if we
simply don't declare their prototypes in .h file?
Many textbooks avoid to discuss this matter and/or discuss only the
usage of static functions. I've been programming for years and I never
felt a real need for the "static approach for functions".
I don't know if this is a trivial matter since the reserved word
exists. :)
Or it exists for some other purpose I can't figure out.
Functions should always be made static if they are not intended as end-
user available routines.
Not quite true: if you have are building a library, you may have
functions in different translation units that you want to be
accessible from all functions within the library, but not
visible outside the library. Declaring such functions
static would limit their scope to the containing translation
unit. In this case, you need to use other "linker magic"
to hide the symbols.

--
Bill Pursell

Mar 6 '07 #22
lc**********@gm ail.com wrote:
<dcor...@connx. comwrote:
.... snip ...
>>
The author of that book wouldn't have been Herbert Schildt, would it?

Well... it's time to study a little more. I have a copy of HS's book
somewhere. :) Perhaps I've missed something the first time I read it.
To get maximum value, find the book and put it to good use as
kindling for the wood stove. DO NOT open it. Then buy something
written by someone who knows his subject.

--
<http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfoc us.com/columnists/423>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
Mar 6 '07 #23
Bill Pursell wrote:
>
.... snip ...
>
Not quite true: if you have are building a library, you may have
functions in different translation units that you want to be
accessible from all functions within the library, but not visible
outside the library. Declaring such functions static would limit
their scope to the containing translation unit. In this case,
you need to use other "linker magic" to hide the symbols.
Very simple:

/* file a.c */
#include "a.h"
#include "alocals.h"
int foo(void) { /* foocode */ }
int bar(void) { /* barcode */ }

/* file a.h */
int foo(void);

/* file alocals.h */
int bar(void);

/* file library.h */
#include "a.h"

The linker doesn't matter.

--
<http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfoc us.com/columnists/423>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
Mar 7 '07 #24
On Mar 6, 10:10 am, lcdgoncal...@gm ail.com wrote:
Hi everyone

Is there a real need to use keyword static with functions, if we
simply don't declare their prototypes in .h file?
Another benefit of marking the function 'static' is that the
compiler can optimise it, since it knows exactly when and how
it is being called. For example it can leave out unused code
branches, use more registers, or even inline the entire
function body.

Mar 7 '07 #25
CBFalconer wrote, On 06/03/07 22:42:
Bill Pursell wrote:
... snip ...
>Not quite true: if you have are building a library, you may have
functions in different translation units that you want to be
accessible from all functions within the library, but not visible
outside the library. Declaring such functions static would limit
their scope to the containing translation unit. In this case,
you need to use other "linker magic" to hide the symbols.

Very simple:

/* file a.c */
#include "a.h"
#include "alocals.h"
int foo(void) { /* foocode */ }
int bar(void) { /* barcode */ }

/* file a.h */
int foo(void);

/* file alocals.h */
int bar(void);

/* file library.h */
#include "a.h"

The linker doesn't matter.
That does not fully hide it. If the user declares a function or variable
with external linkage named bar it still invokes undefined behaviour and
can cause problems, or the user could find out via some other means that
bar exists, write a prototype for it (or not) and call it.

The linker does still matter.
--
Flash Gordon
Mar 7 '07 #26
lc**********@gm ail.com writes:
[...]
Although it's clear for me now that "staticizin g" functions avoids
exporting uncessessary symbols to linker, it's quite unlikely that one
can possibly duplicate a name of a temporary function.
Specially if one give good names for them, with a prefix in a way
similar to Gtk package and similars.
You're asking the wrong question. The question isn't "Why should I
use static?"; it's "Why *shouldn't* I use static?'.

If I were designing the language from scratch today, "static" would be
the default; function names would not be exported beyond file scope
unless you specifically asked for it, probably with an additional
keyword. (Or maybe I'd use some other encapsulation mechanism.)

Declare functions as static unless there's a specific reason not to do
so. ("main" is an odd case; it's usual not to declare it as static.)

--
Keith Thompson (The_Other_Keit h) 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"
Mar 7 '07 #27
Keith Thompson wrote:
>
Declare functions as static unless there's a specific reason not to do
so. ("main" is an odd case; it's usual not to declare it as static.)
I would be interesting trying to build an application if it were!

--
Ian Collins.
Mar 7 '07 #28
"Old Wolf" <ol*****@inspir e.net.nzwrote in message
news:11******** **************@ t69g2000cwt.goo glegroups.com.. .
On Mar 6, 10:10 am, lcdgoncal...@gm ail.com wrote:
>Hi everyone

Is there a real need to use keyword static with functions, if we
simply don't declare their prototypes in .h file?

Another benefit of marking the function 'static' is that the
compiler can optimise it, since it knows exactly when and how
it is being called. For example it can leave out unused code
branches, use more registers, or even inline the entire
function body.
Some compilers may do those things anyways, though if the function is _not_
static then they must still generate the "normal" code in case the function
is called from another translation unit (unless the compiler has what is
often termed "whole-program optimization" and can prove that doesn't occur).

S

--
Stephen Sprunk "Those people who think they know everything
CCIE #3723 are a great annoyance to those of us who do."
K5SSS --Isaac Asimov
--
Posted via a free Usenet account from http://www.teranews.com

Mar 7 '07 #29
On Mar 6, 10:42 pm, CBFalconer <cbfalco...@yah oo.comwrote:
Bill Pursell wrote:

... snip ...
Not quite true: if you have are building a library, you may have
functions in different translation units that you want to be
accessible from all functions within the library, but not visible
outside the library. Declaring such functions static would limit
their scope to the containing translation unit. In this case,
you need to use other "linker magic" to hide the symbols.

Very simple:

/* file a.c */
#include "a.h"
#include "alocals.h"
int foo(void) { /* foocode */ }
int bar(void) { /* barcode */ }

/* file a.h */
int foo(void);

/* file alocals.h */
int bar(void);

/* file library.h */
#include "a.h"

The linker doesn't matter.
In this case, unless I'm misunderstandin g your
post, you have exactly one translation unit which
contains definitions for the functions foo() and
bar(), so this doesn't quite address the situation
I'm describing, which is:

/* file a.c */
int foo(int) __attribute__(( hidden)); (1)
int foo(int x) { /* foo code calls bar*/ }

/* file b.c */
int bar(int) __attribute__(( hidden));
int bar(int x) { /* bar code calls foo*/ }

build library libfoobar.so from a.c and b.c
/* file c.c */
int main(void) { /* can't call foo. */ }

The intention is for foo and bar to only
be visible from functions within the library,
but not be restrained to the translation unit
in which they are defined. Declaring them
static would make foo invisible to bar.

(1) I have no idea how portable the
"__attribute__( (hidden))" is--it works with gcc

--
Bill Pursell

Mar 7 '07 #30

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

Similar topics

3
1607
by: Der Andere | last post by:
Do non-member static functions exist? If yes, what is the sense of making a non-member function a static function? Can these functions only access static variables? Cheers, Matthias Treder
7
2156
by: Nolan Martin | last post by:
is a static functions address constant? ie.. static void func(); write_to_file(&func); Restart program... static void func(); void (*funcPtr) ();
4
1674
by: Ufit | last post by:
What is the difference between those two. Is there gonna be some difference in app execution for static functions? What difference does it make? UF
8
2840
by: prasi | last post by:
hi, all I wanted to know the significance of static functions. can anybody explain me. thanks in advance Prasanna K P
9
2179
by: Pohihihi | last post by:
What could be the possible reasons (technical/non technical) of not using lots of static functions or variables in a program keeping in mind that Framework by itself has tons of static functions and variables?
5
7133
by: WebMatrix | last post by:
Hello, It might seem like a stupid question to some. But I need to put this issue to rest once and for all, since it keeps coming up in code reviews every now and then. There’s a static function in business logic class invoked by a Web multi-user application. Each request calls this static function passing in one unique ID, and XML serialized object (as out param), function deserializes XML to object, creates new instances of DB Access...
7
1896
by: sam_cit | last post by:
Hi Everyone, I had a look at a big project for real-world purpose and i found that all the functions in c have been prototyped as static and i understand that this would mean that these functions can be called only from code within that file, please let me know if there are any other reason to do this.
3
4050
by: llothar | last post by:
I'm using SmallEiffel - which is more or less a huge preprocessor - for C. It has the option to compile the whole Eiffel program into one huge C file but doesn't add a static declaration to the functions. I wonder if there are some clever optimizing compilers that do more magic (for example inlining) of static functions. It's not much time to patch the eiffel compiler, but if it is useless there are other things to waste time.
1
1583
by: wizwx | last post by:
I just noticed an interesting implementation of Singleton from Bruce Eckel's "Thinking in C++" vol. 2, pp 620: class Singleton { static Singleton s; public: static Singleton& instance() { return s; } // .... };
0
9554
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9376
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9923
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9811
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8813
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7358
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3911
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3509
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.