W
wei8010
int fun()
{
static int x = 1000;
return 0;
}
int main( int argc, char* argv[] )
{
cout<<x<<endl;
}
{
static int x = 1000;
return 0;
}
int main( int argc, char* argv[] )
{
cout<<x<<endl;
}
int fun()
{
static int x = 1000;
return 0;
}
int main( int argc, char* argv[] )
{
cout<<x<<endl;
}
int fun()
{
static int x = 1000;
return 0;
}
int main( int argc, char* argv[] )
{
cout<<x<<endl;
[email protected] said:int fun()
{
static int x = 1000;
return 0;
}
int main( int argc, char* argv[] )
{
cout<<x<<endl;
}
[email protected] said: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.
int fun()
{
static int x = 1000;
return 0;
}
int main( int argc, char* argv[] )
{
cout<<x<<endl; <<<<<<<<<<bug here!!!!
}
int fun()
{
static int x = 1000;
return 0;
}
int main( int argc, char* argv[] )
{
printf("%d\n", x);
}
Andrew said:int fun()
{
static int x = 1000;
return 0;
}
int main( int argc, char* argv[] )
{
printf("%d\n", x);
}
You forgot to add
return 0;
before the end of main().
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.