try doing something like this.
#include <iostream>
int main();
using namespace std;
int main()
{}
I learnt most of C++ (whatevr I know) by trying. I dont know what the outcome of the above code will be. You can try and see whether it works.
Some beginner will also be able to answer I guess..
So anyone with the answer please help:
What will be the difference if I write:
#include <iostream>
using namespace std;
int main()
Or,
#include <iostream>
int main()
using namespace std;
That flip in line causes how much problem? and why?
The problem with second one is that the line comes after int main().
int main() means the start of the function main().
After this whatever comes in { } is the definition of the function main() or the program essentially..
In C++, it is required the function definition to follow after the declaration or here int main().
So if you flip the actual method, the declaration comes and the compiler doesnt find any definition and wonders "What the hell happened?" [3]
try doing something like this.
#include <iostream>
int main();
using namespace std;
int main()
{}
I learnt most of C++ (whatevr I know) by trying. I dont know what the outcome of the above code will be. You can try and see whether it works.
First of all, do you know what a namespace is?
A 'namespace' divides the global scope into different subscopes in which you can write different or same lines of code (same lines of code differentiated by their namespaces).
In the iostream header file, the regular operators that you use, such as cout and cin, are stored in the 'std' namespace. If I didn't write using namespace std, I would have to write cout as std::cout (:: is scope resolution operator) and cin as std::cin throughout the program.
Now if we write using namespace std inside the main() function's definition, you won't have to write std::whatever inside the main, but you will have to do so for other functions that you may declare.
#include <iostream>
void poop() //Some function
{
std::cout << "Blah"; //I didn't use the std namespace.
}
int main()
{
using namespace std;
cout << "Blah"; //Used the namespace.
return 0;
}
Thus if I wrote the using namespace part outside all functions(in the global scope of our program), I would not have to use it repeatedly inside my program's different methods/functions. That is why we write it outside.
One more thing. This isn't really an easy kind of question if you've been using C or Turbo C++ instead of the latest ANSI-C++ compilers. It really bugs you. So never underestimate yourself but keep a learning attitude, which you always do, subho :)
^^^^
Now that's some information. But pritish bhaiya, is writing the namespace after a declaration legal? That is if the declaration has no termination?
No 'bhaiya' please....:P
I was assuming subho's main query was that does it make a difference if we put the using namespace line inside or outside the main() function. Which declaration doesn't terminate, Ashish? It's 'using namespace std;' so it has a semi-colon, it does terminate.
I never used the using namespace because I've always used TCC. Gave me some trouble to get used to the compiler used in the INOI.
No I'm talking abt int main()
Is the second piece of code that subho da uploaded valid considering the definition of main follows after the "using namespace"!
Exactly right Pritish is with his post (once again). Just clearing up with term 'namespace' if someone didn't get it. namespaces create the anology of an identifier so as one can use it further in program. Like std::cin is defined in namespace std as a stream named 'cin'. So, 'including' a namespace in program, let's one use it's functions straight away.
Similar is the case with other languages. For e.g: In C#. One "includes" a namespace by using 'Using'.
One of the most basic namespace there is 'System'. So, it is used by:
Using System;
Doing this, basic stream functions like Console.WriteLine can get access straight away.
:D Get any other than Turbo C++. It is horrendously outdated. Get DevCpp IDE (with default MinGW compiler). Works perfectly.
The BCC (Borland C Compiler) works fine - many here prefer it over DEV or TURBO.
I think it is available free on net.