|
|
It could be since you'd call the std library differently. So it would depend on what the book does. But it could eventually become a problem. I would just comment out the namespace line to be safe and go by the book.
But are you aware that devcpp is an abandoned project that isn't updated since 2005? So if your book has an even older version, it must be really really old :p
|
It's been about five years since I did C++, but they look the same to me.
iirc, stdlib.h and cstdlib refer to the same thing, and I think the namespace is declared within the header file.
Also, EXIT_SUCCESS is just a global variable that's defined in cstdlib.
Your book should probably explain these things.
|
It doesn't look like you need the line
#include <iostream> Because you aren't inputting or outputting anything.
As for the
return 0; That ends the program, yes.
EDIT: Oh if you are actually putting more stuff in the program you will need the
#include <iostream> still.
You should probably still include
using namespace std; Because that's what I use in programming and the version I have uses
return 0; There's no harm anyway.
|
Kyrgyz Republic1462 Posts
"using namespace" literally means that you are, well, going to use that name space.
The point is that you may have several classes or functions that have same names, but reside in different namespaces which allows to differentiate between them.
e.g.
namespace A { class X; }
namespace B { class X; }
Then, when you want to refer to a particular class X from one of the namespaces, you use A::X or B::X. This functionality is mostly used by libraries to avoid name clashes with other libraries while still allowing to have good class names, for example "Vector" instead of "libXYZ_Vector".
However, if you are using only one library and don't expect any clashes, you state "using namespace X", and when resolving symbols (class/function names) the compiler would check not only the default namespace, but also namespace X. This allows not to write A::X each time that you want to use class X, but just X.
To answer your question directly, it will not make any difference unless you define a class or function with the exact same name as one of the classes/functions from the std library, which is unlikely.
|
There's no harm in including unused declarations anyway. What I would just put to be safe is
#include <cstdlib> #include <iostream> #include <stdlib.h>
using namespace std;
int main(int argc, char *argv[]) { system("PAUSE"); return 0; }
|
United States3824 Posts
EXIT_Success is going to be 0, the idea being that there were no errors in exiting.
|
cstdlib and stdlib.h are the same thing. I've used them interchangably in my C++ class last semester.
|
The exit success thing is just a global variable. I'm more curious about why there are arguments in the main function. I've never had any, though admittedly it's been like...6-7 years since I did C++.
|
you don't really need them there as far as i'm concerned but the IDE's like to put them there for some reason. maybe someone with more knowledge of the subject can expalin more?
|
ok, here is the thing (it's a little bit too technical, sorry, but if you want to know what's the deal, here is it). C++ is based on C and it keeps backward compatibility with C as much as it is possible. C has these fancy libraries exposed through stdio.h, stdlib.h, stdarg.h, etc.
C++ (BS indeed) decided to make these include things to be abstracted from filesystems, etc, so the ".h" psotfix would not make sense right? that's why they changed all those NAMEs to cNAME, like cstdio, cstdarg, etc. now these files expose the old interfaces through the namespace std.
so #include <stdio.h>
is completely equivalent to #include <cstdio> using namespace std;
The latter one is preferable, because it's C++, not C, and it's going to be always supported (though unlikely that the former one will ever be dropped). The C++ thing is preferable also because it does not pollute the global namespace with old C things. So the better way is to do
#include <cstdio>
and use std::printf("%s", "I'm a good C++ fellow");
lol who asked me to jump in and write all this? :S
oh yeah, btw, cxxx files are mostly implemented like this: // file cxxx namespace std { #include <xxx.h> }; //namespace std
|
yeah that is weird 0.o
as far as the cstdlib and stdlib.h, if i remember correctly in C you use the library stdlib.h but when you use C++ all the old c libraries were renamed as clibraryname. so stdlib.h became cstdlib, math.h became cmath etc. but i havent c++ed in a while so i might be off
edit: you beat me to it
|
holy fuck error(s) when I changed it the way the book was =/
0 errors on this, maybe =/
#include <cstdlib> #include <iostream>
using namespace std;
int main(int argc, char *argv[]) { cout << "Hello, I am your computer talking." << endl; system("PAUSE"); return EXIT_SUCCESS; }
|
iono wat using namespace std; is but EXIT_SUCCESS is the same thing as returning 0 if i remember correctly... also all it does is turn on some flag saying that the method exited successfully which will keep a handler from catching some kind of error and throwing u an exception
|
Chapter 1 finished. 772 pages left to go.
|
So there is no difference between, they are both the same thing correct?
system("PAUSE"); return 0; }
and
system("PAUSE"); return EXIT_SUCCESS; }
???
|
On June 03 2009 16:17 {CC}StealthBlue wrote:So there is no difference between, they are both the same thing correct? and ??? Do you understand what a function is right? main is a function with return value of type int. Whatever this value is, it won't change anything. It can be 0, 1, 2, ... it doesn't matter, however, using 0 is just a convention so as to say "The function returned successfully."
In the above example, EXIT_SUCCESS must be a global variable of type int whose value could be any number (most likely 0), so in the end, it's just the same thing.
I hope this helped.
|
#define EXIT_SUCCESS 0 #define EXIT_FAILURE 1
don't use those definitions, don't depend on stdlib.h. do return 0; instead
|
On June 01 2009 03:20 {CC}StealthBlue wrote:So I downloaded Dev-C++ from the net but is an further updated version in the book and the new project is different that the one being shown in the book, at the start. http://search.barnesandnoble.com/C-All-in-One-Desk-Reference-for-Dummies/Jeff-Cogswell/e/9780764517952/?itm=2Show nested quote +#include <cstdlib> #include <iostream>
using namespace std;
int main(int argc, char *argv[]) { system("PAUSE"); return EXIT_SUCCESS; }
^Is showing when I create a new project but in the book the figure is showing this: Show nested quote +#include <iostream> #include <stdlib.h>
int main(int argc, char *argv[]) { system("PAUSE"); return 0; }
Is this going to cause trouble or even affect the step by step tutorials in the book? no. you'll learn letter about macros and const variables. EXIT_SUCCESS actually = 0 for learning purposes though go with return 0 unless you have other reasons not to.
|
|
|
|