|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
static void ?
what is the difference between
Code:
void main( ...) and Code:
static void main( ...) in C |
|
#2
|
|||
|
|||
|
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. |
|
#3
|
||||
|
||||
|
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:
Any questions? ![]()
__________________
"A poor programmer is he who blames his tools." http://analyser.oli.tudelft.nl/ |
|
#4
|
|||
|
|||
|
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 ? |
|
#5
|
|||
|
|||
|
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. |
|
#6
|
||||
|
||||
|
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.
|
|
#7
|
|||
|
|||
|
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. |
|
#8
|
||||
|
||||
|
Re: static void ?
Quote:
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:
So it's not just a "would never want to", but also a "just plain won't work". |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > static void ? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|