30
Ashish Kothari
·2011-06-02 09:03:08
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]
30
Ashish Kothari
·2011-06-02 09:10:06
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.
39
Pritish Chakraborty
·2011-06-02 09:13:16
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 :)
30
Ashish Kothari
·2011-06-02 09:21:52
^^^^
Now that's some information. But pritish bhaiya, is writing the namespace after a declaration legal? That is if the declaration has no termination?
39
Pritish Chakraborty
·2011-06-02 09:25:42
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.
30
Ashish Kothari
·2011-06-02 09:27:17
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.
30
Ashish Kothari
·2011-06-02 09:29:11
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"!
1
Ankur
·2011-06-02 10:04:58
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.
49
Subhomoy Bakshi
·2011-06-02 13:45:56
suggest me a good C++ compiler.. (ANSI hona zaruri hai) :P
1
Ankur
·2011-06-02 16:20:47
:D Get any other than Turbo C++. It is horrendously outdated. Get DevCpp IDE (with default MinGW compiler). Works perfectly.
49
Subhomoy Bakshi
·2011-06-03 04:50:49
arey koi lin wagera do na...[3]
aise life hell hai! :P
11
Devil
·2011-06-03 09:43:52
The BCC (Borland C Compiler) works fine - many here prefer it over DEV or TURBO.
I think it is available free on net.