Why does this work?!?

M

Mark Bruno

I thought system("PAUSE"); was part of stdlib.h and cstdlib. Then why does
this work? :

#include <iostream>
using namespace std;
int main()
{
cout << "hello, world" << endl;
system("PAUSE");
return 0;
}

It seems that system is part of iostream?!? that's news to me!
 
J

Jack Klein

I thought system("PAUSE"); was part of stdlib.h and cstdlib. Then why does
this work? :

#include <iostream>
using namespace std;
int main()
{
cout << "hello, world" << endl;
system("PAUSE");
return 0;
}

It seems that system is part of iostream?!? that's news to me!

Just one of the quiet changes in C++ from C. The C language standard
specifically prohibits any standard header from including any other
standard header. The C++ language standard, OTOH, specifically allows
ANY standard header to include any or all other standard headers at
the discretion of the implementors. So <iostream> including <cstdlib>
or <stdlib.h> is perfectly legal behavior for your compiler.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
J

John Carson

Mark Bruno said:
I thought system("PAUSE"); was part of stdlib.h and cstdlib. Then
why does this work? :

#include <iostream>
using namespace std;
int main()
{
cout << "hello, world" << endl;
system("PAUSE");
return 0;
}

It seems that system is part of iostream?!? that's news to me!

This may vary from one system to another, but on VC++ 7.0:

iostream includes istream
istream include ostream
ostream includes ios
ios includes xlocnum
xlocnum includes cstdlib
 
T

Thomas Wintschel

Mark Bruno said:
I thought system("PAUSE"); was part of stdlib.h and cstdlib. Then why does
this work? :

#include <iostream>
using namespace std;
int main()
{
cout << "hello, world" << endl;
system("PAUSE");
return 0;
}

It seems that system is part of iostream?!? that's news to me!

try tracing the #includes from <iostream>
 
M

Mark Bruno

Thanks guys, you explained it perfectly. I guess I should always add
#include <cstdlib> in such a scenario for compiler compatibility?
 
J

John Carson

Mark Bruno said:
Thanks guys, you explained it perfectly. I guess I should always add
#include <cstdlib> in such a scenario for compiler compatibility?

In order to guarantee that system will be declared, yes.
 

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
473,778
Messages
2,569,605
Members
45,237
Latest member
AvivMNS

Latest Threads

Top