2

I got both the codes from Books from an Online PDF

First -

#include <iostream>
int main()
{
std::cout << "Hello World!";
return 0;
}

Second -

#include <iostream>
using namespace std;
int main()
{
    cout << "Hello world!" ;
    return 0;
}
Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
  • 1
    @S.M. - i honestly don't see anything wrong with pulling entire std in, as long as its done in your cpp, NOT in the header. Now, pulling it for me in a header, in a huge project, and causing a huge pain in the butt, just because one is too lazy to type std:: a couple of times (or add specific using-s), now thats a different story. But really, in an 'academic' code, does it really matter? – Boris Lipschitz Jul 10 '20 at 05:28

1 Answers1

1

No difference, using namespace std; simply means everything that is otherwise available via std namespace no loner needs the std:: prefix. In a cpp file its a personal preference. In an h file - don't use using namespace std;, this is because std namespace is huge, and you may be not the only one including that h. For a beginner, or 'academic' code in general it doesn't really matter, but believe me, when you are on the receiving end of someone pulling the entire std namespace in on you in a big project, you aren't gonna like it.

Boris Lipschitz
  • 1,514
  • 8
  • 12
  • So , Should I use 2nd one?? – Shivam Yadav Jul 10 '20 at 05:26
  • Typically, no, do not use `using namespace std;` – Derek C. Jul 10 '20 at 05:27
  • If unsure about the consequences - use the first one. Here - doesn't matter at all. – Boris Lipschitz Jul 10 '20 at 05:29
  • @DerekC.why not? As long as it isn't in a common header, and only affects my code, how is it not just a personal preference? Yea, i know there are huge religious threads about this, with claims such as "what about the next cpp version that may add some stuff to std, that may create contentions" and other similar claims... well, while this is correct in theory, if thats your only problem proting your code over - consider yourself lucky :) – Boris Lipschitz Jul 10 '20 at 05:32
  • @BorisLipschitz I already solved about 5 problems on Stackoverflow where the problem was that someone used a name for a token that is already used in STL. There are probably hundreds of questions. It's nearly impossible for a beginner to know all names. You can e.g. search for questions with C++ and "swap" and you will find many reasons to not use `using namespace std;` – Thomas Sablik Jul 10 '20 at 07:08
  • The next candidate for "Don't use `using namespace std;`" https://stackoverflow.com/questions/62837273/c-program-is-not-running. – Thomas Sablik Jul 10 '20 at 15:19
  • @ThomasSablik, of course it can easily create contentions between stl names and your own names. Its also nearly impossible to a non-beginner to know all names. But its ultimately up to you if you prefer to deal with that, type `std::` everywhere you use stl, or add a separate `using...` for everything you use. Now if you end up pulling an entire std namespace and then failing to realize whats going on, and actually ask about it on SO... well, thats a PEBKAC issue, not a technical issue. As long as you don't put it in the header that i am going to include and get hit by it :-) – Boris Lipschitz Jul 10 '20 at 20:07
  • This sound to me like "I don't need a seatbelt and airbag because I know how to drive a car": A fact is `using namespace std;` causes problems (at least to some beginners) that can be avoided simply by not using `using namespace std;`. It's the same problem with raw `new`/`delete`. If you handle it correctly there are no problems. But you should avoid it because it can cause problem that could be simply avoided. – Thomas Sablik Jul 12 '20 at 22:10