473,396 Members | 1,712 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,396 software developers and data experts.

static or non static

this is probably only a stylistic question

what do you think to be better in a single .c file program, declaring
functions with static qualifier or without it?

eg:
#include <stdio.c>
static int add(int a, int b) {return a+b;}
int main() {
printf("1+2=%d\n", add(1, 2));
return 0;
}

is there any difference between the two?

static int add(..)
or
int add(..)

Sep 17 '07 #1
7 2448
Szabolcs Nagy wrote:
this is probably only a stylistic question

what do you think to be better in a single .c file program, declaring
functions with static qualifier or without it?

eg:
#include <stdio.c>
static int add(int a, int b) {return a+b;}
int main() {
printf("1+2=%d\n", add(1, 2));
return 0;
}

is there any difference between the two?

static int add(..)
or
int add(..)
Always use static for functions that are not referenced outside the
module in which they are defined. This helps to keep name conflicts low.

Note that function names are exported by default in C.

Besides,

If a function is not used and it is declared static, the compiler
will warn you. If it is not, the compiler can't know if it is used in
another module and you end up with many functions unused completely
taking place for nothing at all.


Sep 17 '07 #2
Szabolcs Nagy <ns*******@gmail.comwrites:
what do you think to be better in a single .c file program, declaring
functions with static qualifier or without it?
I would generally use static. Single-file programs tend to grow,
and it's easier if they're done "properly" from the start.
--
"To get the best out of this book, I strongly recommend that you read it."
--Richard Heathfield
Sep 17 '07 #3
Szabolcs Nagy <ns*******@gmail.comwrites:
this is probably only a stylistic question

what do you think to be better in a single .c file program, declaring
functions with static qualifier or without it?

eg:
#include <stdio.c>
static int add(int a, int b) {return a+b;}
int main() {
printf("1+2=%d\n", add(1, 2));
return 0;
}

is there any difference between the two?

static int add(..)
or
int add(..)
In theory I would recommend marking them all as static. In practice,
well, I tend to keep a specific "extern" header for others to include
since more functions than not are often used outside of the file they
are defined in.
Sep 17 '07 #4
Szabolcs Nagy wrote:
>
what do you think to be better in a single .c file program,
declaring functions with static qualifier or without it?

eg:
#include <stdio.c>
static int add(int a, int b) {return a+b;}
int main() {
printf("1+2=%d\n", add(1, 2));
return 0;
}

is there any difference between the two?

static int add(..)
or
int add(..)
The advantage of the 'static' is that you can later decide to
expand the system with a separate file, and not worry about the
fact that 'add' has already been defined. static makes it only
visible within the original file.

Using add() in an external C file requires all of 1. removal of the
static and 2. inclusion of a prototype in main.h, and 3. #include
"main.h" in the new file.

--
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 17 '07 #5
Szabolcs Nagy wrote:
this is probably only a stylistic question

what do you think to be better in a single .c file program, declaring
functions with static qualifier or without it?

eg:
#include <stdio.c>
Unusual header name ...
static int add(int a, int b) {return a+b;}
int main() {
printf("1+2=%d\n", add(1, 2));
return 0;
}

is there any difference between the two?

static int add(..)
or
int add(..)
Use `static', even in single-module programs. Even if
the program never grows beyond a single file, the fact that
the compiler can "see" all the calls to add() may allow the
compiler to optimize it more aggressively, for example, by
compiling it in-line.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Sep 18 '07 #6

Eric Sosman wrote:
Szabolcs Nagy wrote:
static int add(..)
or
int add(..)

Use `static', even in single-module programs. Even if
the program never grows beyond a single file, the fact that
the compiler can "see" all the calls to add() may allow the
compiler to optimize it more aggressively, for example, by
compiling it in-line.
thank you all the answers
i guess static is nicer solution then

but can it really matter with respect to the optimization?
may be the .o code contains the extern functions but what about the
executable?
i thought in situations like this (single-module program) the compiler
automatically handles every function as static in the final
executable.
as a final note:
i checked the src of traditional unix tools (which are mostly single-
modul programs) and found both conventions to be used (static and
extern functions)
also note that c-faq contains very little amount of information about
'static' (i'm not complaining i just expected more questions)

Sep 18 '07 #7

Eric Sosman wrote:
[snip]
thanks your comment was very helpful

Sep 18 '07 #8

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

Similar topics

8
by: Scott J. McCaughrin | last post by:
The following program compiles fine but elicits this message from the linker: "undefined reference to VarArray::funct" and thus fails. It seems to behave as if the static data-member:...
15
by: Samee Zahur | last post by:
Question: How do friend functions and static member functions differ in terms of functionality? I mean, neither necessarily needs an object of the class to be created before they are called and...
5
by: Mountain Bikn' Guy | last post by:
How would I do this? public sealed class UtilityClass { public static MyObject Object1;//see note below about importance of static object names in this class public static MyObject Object2;...
3
by: Jay | last post by:
Why are there static methods in C#. In C++ static was applied to data only (I believe) and it meant that the static piece of data was not a part of the object but only a part of the class (one...
9
by: Laban | last post by:
Hi, I find myself using static methods more than I probably should, so I am looking for some advice on a better approach. For example, I am writing an app that involves quite a bit of database...
12
by: Joe Narissi | last post by:
I know how to create and use static constructors, but is there a such thing as a static destructor? If not, then how do you deallocate memory intialized in the static constructor? Thanks in...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
1
by: Sandro Bosio | last post by:
Hello everybody, my first message on this forum. I tried to solve my issue by reading other similar posts, but I didn't succeed. And forgive me if this mail is so long. I'm trying to achieve the...
9
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to...
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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
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...
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...
0
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,...

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.