473,796 Members | 2,483 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why not compile?


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

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

Aug 4 '06 #1
7 1807
In article <11************ *********@i3g20 00cwc.googlegro ups.com>,
we*****@gmail.c om <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

we*****@gmail.c om 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
"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.
Aug 4 '06 #4

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_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.
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 "initialize d at runtime"and "initialize d
with compile-time",can you help me?
Thanks!

Aug 4 '06 #5
we*****@gmail.c om 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
On 2006-08-04, Keith Thompson <ks***@mib.orgw rote:
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
Andrew Poelstra wrote:
On 2006-08-04, Keith Thompson <ks***@mib.orgw rote:
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2817
by: janeaustine50 | last post by:
Python's InteractiveInterpreter uses the built-in compile function. According to the ref. manual, it doesn't seem to concern about the encoding of the source string. When I hand in an unicode object, it is encoded in utf-8 automatically. It can be a problem when I'm building an interactive environment using "compile", with a different encoding from utf-8. IDLE itself has the same problem. ( '<a string with non-ascii-encoding>' is...
5
3337
by: Carmine Cairo | last post by:
Hi, I'm working on a project and today I've note a little problem during the compile fase. Here a little piece of code: // 1st version welldone = 0; size = p->getSize(); backbone = new rightType;
5
3823
by: Brice Prunier | last post by:
Here under 4 schemas i'm working with ( it may be long: sorry...) The context is the following : Resident.xsd imports Person.xsd and includes Common.xsd ( anonimous schema: no TargetNamespace ) Person.xsd includes Common-Naming.xsd ( anonimous schemas ) Common-Naming.xsd includes common.xsd ( both are anonimous schemas ) Compilation of Resident.xsd raise the following exception: "System.Xml.Schema.XmlSchemaException: The attribute 'oid'...
10
19730
by: Chris LaJoie | last post by:
Our company has been developing a program in C# for some time now, and we haven't had any problems with it, but just last night something cropped up that has me, and everyone else, stumped. I have a struct that contains several different types of data. This struct is used throuout the program. Now, when I compile, I get 6 errors, all of them "Use of possibly unassigned field 'awayTime'" or "Use of possibly unassigned field 'intlTime'"....
6
2861
by: Thomas Connolly | last post by:
I have 2 pages referencing the same codebehind file in my project. Originally the pages referenced separate code behind files. Once I changed the reference to the same file, everything worked fine while the file was in the project directory. When the obsolete file was removed from the project directory, my application will no longer compile. Can someone please help with this issue? Thank in advance, Tom
15
4856
by: steve yee | last post by:
i want to detect if the compile is 32 bits or 64 bits in the source code itself. so different code are compiled respectively. how to do this?
16
5434
by: desktop | last post by:
I have read that using templates makes types know at compile time and using inheritance the types are first decided at runtime. The use of pointers and casts also indicates that the types will first be know at runtime. But is there some strict definitions that defines runtime code and compile time code that can be used in general?
1
3082
by: brianrpsgt1 | last post by:
Newbie here.... I have been able to successful pull info from a MySQL DB, get the results and output them in an HTML format using Cheetah to the screen using IDLE. I am doing this on a Windows Laptop, running WinXP, Python 2.5 and the latest version of Cheetah. I have two questions: 1. How and where do you compile Cheetah templates in Windows? The command in the docs is cheetah compile a, however, I believe that this
3
2311
by: NvrBst | last post by:
Right now I have C99 code in .c extensions. I compile it in VSC++ and it complains about a lot of errors. I change the extensions to .cpp and compile in VSC++ and it succeeds. Is there a way to keep my extensions .c, but tell VSC++ (through an project option) to compile as C++98 code (as if it had .cpp extensions).
6
1959
by: Ed Leafe | last post by:
I've noticed an odd behavior with compile() and code that does not contain a trailing newline: if the last line is a comment inside of any block, a syntax error is thrown, but if the last line is a non- comment Python statement, there is no error. Here's an example (using 2.5.1 on OS X) .... def foo(): .... print 'bar' """ <code object <moduleat 0x79608, file "", line 2>
0
9528
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10455
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
7547
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6788
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5441
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4116
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2925
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.