For a beginner, is it okay not to bother about the syntax of preprocessor directives while learning to write C++ codes? I find online consoles each with a different default syntax to start with. For instance one of them has this one:
1 2 3 4 5 6 7 8 9 10 11 12
// Example program
#include <iostream>
#include <string>
int main()
{
std::string name;
std::cout << "What is your name? ";
getline (std::cin, name);
std::cout << "Hello, " << name << "!\n";
}
Another popular online console has this one:
[/code]
#include <iostream>
using namespace std;
int main() {
// your code goes here
return 0;
}
[/code]
I mean since iostream file is included, is there still need to include std:: part.
That has nothing to do with including files. That is because those types exist in the std:: namespace.
Note that the second example has:
usingnamespace std;
This pulls the entirety of the std:: namespace into the global namespace. So in that second example, you could reference string and cout directly, without specifying a namespace.
It's not generally considered a good thing to do in production code. Many teachers consider it a helpful thing to do when teaching beginners, because it avoids having to explain namespaces until later.
Thanks @MikeyBoy. One thing where I am facing problem is that it is time and again mentioned that main function should return a value of 0 if successful. Now, for this program, without return 0, successful:
1 2 3 4 5 6 7 8
// Example program
#include <iostream>
usingnamespace std;
int main()
{
cout << "What is your name? ";
}
main() is a special function. If you don't explicitly return anything it will automatically return the value 0. This is not the case for other functions. A non-void function other than main() should always return something.
Thanks @Peter87. Sorry for my understanding. I could see only main() as the function. So, please help me out which is the other function here such that there is no need to put return 0. Also, wny author Bjarne Stroustrup had included return 0 in the below program as I could see the program running without putting return 0 as well
the rule is, if your function declaration returns a type, then the function body must actually return a value of that type. the compiler will complain otherwise.
HOWEVER.
a special case exists because "main" is already known to the compiler, when a program starts executing it does some housework before calling your code to execute, therefore your code must always start at main(), the "housework" code or "startup" code as we call it, is hard coded to call something called "main". if you dont provide "main" you will get a linker error where the "startup" code is trying link (patch in) system code to your code.
TO YOUR POINT
for some reason, people who think they are clever decided that the compiler should make a special case for the main() function and if you don't code a return statement it will stick one in for you silently. To me, this is so wrong, a rule should be a rule, special cases just cause confusion.
I could see only main() as the function. So, please help me out which is the other function here such that there is no need to put return 0.
In that piece of code you have only defined one function, which is main(). I was just talking in general. I didn't want you to think the same applied to other functions, if you where to define other functions, because that would be a mistake.
bagrarajeev wrote:
wny author Bjarne Stroustrup had included return 0 in the below program as I could see the program running without putting return 0 as well
There's nothing wrong with return 0; in main(). I don't know the reason why he used it but maybe it's to avoid confusion. If he left it out he would have to do what I do now, explain why leaving out return from main() is fine but with other functions it's not.
Jaybob66 wrote:
if your function declaration returns a type, then the function body must actually return a value of that type. the compiler will complain otherwise
Note that with some compilers you need to turn on extra warnings in order to get it to "complain" about missing return statement. With GCC it's one of many useful warnings that is turned on by the -Wall compiler flag.