"we*****@gmail. com" <we*****@gmail. comwrites:
int fun()
{
static int x = 1000;
return 0;
}
int main( int argc, char* argv[] )
{
cout<<x<<endl;
}
cout, endl, and the "<<" operator used for output are all specific to
C++, a language discussed in comp.lang.c++.
But let's assume you instead wrote this in C:
int fun()
{
static int x = 1000;
return 0;
}
int main( int argc, char* argv[] )
{
printf("%d\n", x);
}
Now you've still got a problem: there's no visible prototype for the
printf function, so this would invoke undefined behavior even if it
compiled. You need to add "#include <stdio.h>". <OT>You'd need some
C++ header for your original program; don't ask me which one.</OT>
You tell us this doesn't compile, but you don't tell us *how* it
doesn't compile. If you have a question like this, it's best to quote
the actual error message you received. (You should also include the
question in the body of the article; not all readers can easily see
the subject header along with the article.)
But I think what you're actually asking about is that the compiler
complains about the reference to x in main(). You declared x as a
static variable in fun(), so the name "x" is only visible inside
fun(). If you want x to be visible in both fun() and main(), you need
to declare it outside any function.
--
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.