By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,952 Members | 1,816 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,952 IT Pros & Developers. It's quick & easy.

Why not compile?

P: n/a

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

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

Aug 4 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
In article <11*********************@i3g2000cwc.googlegroups.c om>,
we*****@gmail.com <we*****@gmail.comwrote:
>int fun()
{
static int x = 1000;
return 0;
}
>int main( int argc, char* argv[] )
{
cout<<x<<endl;
}
Please try the C++ newsgroup comp.lang.c++

--
All is vanity. -- Ecclesiastes
Aug 4 '06 #2

P: n/a

we*****@gmail.com wrote:
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.
}
Aug 4 '06 #3

P: n/a
"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_Keith) 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.
Aug 4 '06 #4

P: n/a

Keith Thompson 写道:
"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_Keith) 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.
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!

Aug 4 '06 #5

P: n/a
we*****@gmail.com 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.
Aug 4 '06 #6

P: n/a
On 2006-08-04, Keith Thompson <ks***@mib.orgwrote:
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().

--
Andrew Poelstra <http://www.wpsoftware.net/projects>
To reach me by email, use `apoelstra' at the above domain.
"Do BOTH ends of the cable need to be plugged in?" -Anon.
Aug 4 '06 #7

P: n/a
Andrew Poelstra wrote:
On 2006-08-04, Keith Thompson <ks***@mib.orgwrote:
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.

Aug 4 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.