The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
static void ?
Discuss static void ? in the C Programming forum on Dev Shed. static void ? C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 25th, 2003, 01:29 AM
|
|
Contributing User
|
|
Join Date: Feb 2003
Location: Belgium
Posts: 60
Time spent in forums: < 1 sec
Reputation Power: 11
|
|
|
static void ?
what is the difference between
and
Code:
static void main( ...)
in C
|

March 25th, 2003, 03:31 AM
|
|
Contributing User
|
|
Join Date: Feb 2001
Posts: 1,365

Time spent in forums: 18 h 9 m 25 sec
Reputation Power: 14
|
|
|
Hmmm...I don't think that's a proper use of static. The static keyword is usually used with classes. A static variable or function exists even if no objects of the class exist, and all objects share the static variable or function. If you change a static member variable of an object, all the objects reflect that change. You could use a static member variable to keep track of how many objects of the class have been created.
You can also use static variables inside regular functions. They continue to exist even after the function executes and all the local variables are destroyed. Sometimes they're used to count how many times the function was called.
Last edited by 7stud : March 25th, 2003 at 03:34 AM.
|

March 25th, 2003, 04:04 AM
|
 |
*bounce*
|
|
Join Date: Jan 2002
Location: Delft, The Netherlands
|
|
He was talking about C, 7stud, not C++. C doesn't even have classes... You're right about the static variables though
A static function's scope is limited to the file it's defined in; only functions in the same file can "see" (and hence call) it. Technically, what it does is inhibit the export of the function name to the linker.
The static keyword is called a storage class, and a function has any one of the following three (excerpt from H&S):
Quote:
auto -- Permitted only in declarations of variables at the heads of blocks. It indicates that the variable has local (automatic) extent. (Because this is the default, auto is rarely seen in C programs.)
extern -- May appear in declarations of external functions and variables, either at the top level or at the heads of blocks. It indicates that the object declared has static extent and its name is known to the linker.
static -- May appear on declarations of functions or variables. On function definitions, it is used only to specify that the function name is not to be exported to the linker. On function declarations, it indicates that the declared function will be defined--with storage class static--later in the file. On data declarations, it always signifies a defining declaration that is not exported to the linker. Variables declared with this storage class have static extent (as opposed to local extent, signified by auto). |
Any questions? 
__________________
"A poor programmer is he who blames his tools."
http://analyser.oli.tudelft.nl/
|

March 25th, 2003, 05:46 AM
|
|
Contributing User
|
|
Join Date: Feb 2003
Location: Belgium
Posts: 60
Time spent in forums: < 1 sec
Reputation Power: 11
|
|
|
so basicly a static function in C
can only be used in the same *c file ?
what is the use of this static function then ?
|

March 25th, 2003, 07:48 AM
|
|
|
|
This behavior extends to data declared outside of any function as well. Static variables declared outside of a function can only be used by functions in that file. In Visual C++ declaring main as a static will have no effect since the compiler will ignore the static and just generate a warning.
The fact that C makes everything global by default and requires that we use static to localize things is backwards in my opinion. We should have to explicitly make data and functions global in scope. Doing it the way C does just promotes bad programming practice.
Last edited by 3dfxMM : March 25th, 2003 at 07:52 AM.
|

March 25th, 2003, 08:24 AM
|
 |
*bounce*
|
|
Join Date: Jan 2002
Location: Delft, The Netherlands
|
|
|
The use of static for functions and global variables is to avoid name clashes in the object code's symbol tables. This way, you can still have two or more functions with the same name.
|

March 25th, 2003, 09:08 AM
|
|
Junior Member
|
|
Join Date: Mar 2003
Posts: 11
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
void!??
According ISO/IEC standard C++ of 1998 main is int.
void is available old type.
I never heared about static notification using with
non-class member functions.
|

March 25th, 2003, 10:02 AM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
|
Re: static void ?
Quote: Originally posted by Jonas_G
what is the difference between
and
Code:
static void main( ...)
in C |
A quick note that hadn't been brought up yet.
main() is the program's entry point and, as such, must be made known to the linker for the start-up code to be able to link to it. Therefore, one would never want to make main() static -- unless possibly to have a function in another module called "main", but that just seems a bit too weird.
As an experiment, I made a program's main() function static and got this error from gcc:
Quote:
c:/fsf/lib\crt0.o(.data+0x92):crt0.s: undefined reference to `main'
c:/fsf/lib/libc.a(crt1.o)(.text+0x312):crt1.c: undefined reference to `main' |
So it's not just a "would never want to", but also a "just plain won't work".
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|