Why not compile?

W

wei8010

int fun()
{
static int x = 1000;
return 0;
}

int main( int argc, char* argv[] )
{
cout<<x<<endl;
}
 
S

Scorpio

int fun()
{
static int x = 1000;
return 0;
}

int main( int argc, char* argv[] )
{
cout<<x<<endl;

Because this is not C language code.
Go read some good books before trying to write code.
 
K

Keith Thompson

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

wei8010

Keith Thompson 写é“:
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.

Thanks for your answer!
I am a programer from Chinese,My English is poor!

Above program , I seen from <<Modern C++ Desingn>> chapter 6.4.

" A function-static object is initialized when the control flow is
first passing its definition. Don't confuse static variables that are
initialized at runtime with primitive static variables initialized with
compile-time constants. "

I can't understand between "initialized at runtime"and "initialized
with compile-time",can you help me?
Thanks!
 
J

jacob navia

(e-mail address removed) a écrit :
int fun()
{
static int x = 1000;
return 0;
}

int main( int argc, char* argv[] )
{
cout<<x<<endl; <<<<<<<<<<bug here!!!!
}

The compiler told you the problem. Fix it
instead of asking in newsgroups the obvious.

The "x" there is not defined anywhere.

Why?

Learn about "scope of identifiers" using your
C (or C++) book.
 
A

Andrew Poelstra

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().
 
G

Guest

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().

In both C99 and C++, not returning anything on the initial call to
main() is equivalent to returning 0. In C89, it would be a good idea,
though.
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,188
Messages
2,571,002
Members
47,591
Latest member
WoodrowBut

Latest Threads

Top