C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 25th, 2003, 01:29 AM
Jonas_G Jonas_G is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Belgium
Posts: 60 Jonas_G User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 11
static void ?

what is the difference between
Code:
void main( ...)

and
Code:
static void main( ...)

in C

Reply With Quote
  #2  
Old March 25th, 2003, 03:31 AM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,365 7stud User rank is Private First Class (20 - 50 Reputation Level)7stud User rank is Private First Class (20 - 50 Reputation Level) 
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.

Reply With Quote
  #3  
Old March 25th, 2003, 04:04 AM
Analyser's Avatar
Analyser Analyser is offline
*bounce*
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2002
Location: Delft, The Netherlands
Posts: 513 Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level)Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level)Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level)Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level)Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level)Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 22 h 45 m 12 sec
Reputation Power: 41
Send a message via ICQ to Analyser
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/

Reply With Quote
  #4  
Old March 25th, 2003, 05:46 AM
Jonas_G Jonas_G is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Belgium
Posts: 60 Jonas_G User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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 ?

Reply With Quote
  #5  
Old March 25th, 2003, 07:48 AM
3dfxMM 3dfxMM is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 272 3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 6 Days 17 h 57 m 24 sec
Reputation Power: 17
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.

Reply With Quote
  #6  
Old March 25th, 2003, 08:24 AM
Analyser's Avatar
Analyser Analyser is offline
*bounce*
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2002
Location: Delft, The Netherlands
Posts: 513 Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level)Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level)Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level)Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level)Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level)Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 22 h 45 m 12 sec
Reputation Power: 41
Send a message via ICQ to Analyser
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.

Reply With Quote
  #7  
Old March 25th, 2003, 09:08 AM
Doctor - A Doctor - A is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 11 Doctor - A User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #8  
Old March 25th, 2003, 10:02 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is online now
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,136 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 21 h 10 m 40 sec
Reputation Power: 1974
Re: static void ?

Quote:
Originally posted by Jonas_G
what is the difference between
Code:
void main( ...)

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".

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > static void ?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap