0
C++ beginner questions
I just started learning C++ and when I code, it always starts with: #include <iostream> using namespace std; Why is that? What does it do and what does it mean? Do I always have to start that way when coding? Another question is about int main () Why do I declare main as an integer like I would for an assigned value?
2 Respuestas
+ 6
What is <iostream>?
https://www.sololearn.com/Discuss/232791/?ref=app
https://www.sololearn.com/Discuss/76430/?ref=app
What is the std namespace?
https://www.sololearn.com/Discuss/93433/?ref=app
https://www.sololearn.com/Discuss/1518433/?ref=app
Why does main() return int?
https://www.sololearn.com/Discuss/476915/?ref=app
https://www.sololearn.com/Discuss/1225/?ref=app
https://www.sololearn.com/Discuss/777127/?ref=app
+ 5
Aaaaaand, yes and no.
If you read those threads provided, you should understand that, in short:
- <iostream> is responsible for providing the standard input/output functionalities for your program.
- "using namespace std" specifies that certain objects/functions you use, like cout and cin, are from the std namespace. (std::cout, std::cin)
This means that if your program does not deal with input and output streams, you don't need to use <iostream>. If you can specify the std namespace in front of every standard object, then "using namespace std" is not required. You can have a program which just do:
int main() {
int a = 39;
}
The above is definitely a valid program, but without <iostream>, there's not much to do without input and output, right?
As for main() returning integer, it is specified as part of the language standard. It is compulsory for main to return int.
http://www.stroustrup.com/bs_faq2.html#void-main