Why do you need both? I know this is a real newb question, but it's really been bothering me lately. What's the difference, and why do I need both to use cin and cout? Thanks in advance.
Printable View
Why do you need both? I know this is a real newb question, but it's really been bothering me lately. What's the difference, and why do I need both to use cin and cout? Thanks in advance.
You mean why do you need both
#include <iostream>
and
using namespace std;
?
That's because they do different things. The first actually includes the definitions. If you have only the #include you already use cin and cout, but they are in the namespace std, so you have to refer to them by full name:
std::cout << "Hello, World!" << std::endl;
The using thing imports the symbols from iostream into the global namespace so that you don't need the std:: to reference them.
So 'using namespace std' just says that your going to be using stuff from std.
Another question, what are symbols (hey, I'm on a roll here :) )?
Nearly. You can use stuff from std whether you write that or not. It tells the compiler that you won't explicitly say that the stuff you use is in std.Quote:
So 'using namespace std' just says that your going to be using stuff from std.
Symbol is the common name for variable, constant, function, class etc. names.Quote:
Another question, what are symbols?
So types = symbols?
No, the type names are symbols.
There are 5 parts of code: keywords, operators, literals, symbols and comments.
Comments are lines that start with // or blocks enclosed by /* ... */.
Keywords are all reserved words: int, float, if, while, break, return, class...
Literals are explicit values: 4, 23.1, 0xe3, 'A', "Hello"
Operators are, well, operators: +, -, %, &, *, ...
Symbols are everything else. In
int is a keyword, [] is an operator, 12 is a literal. /* ... */ and // ... are comments. my_array is a newly defined symbol. Whenever after it appears in the code the compiler knows that you are referring to that array.Code:/* Define an array */
int my_array[12]; // Array is 12 elements large
Thanks for clearing the confusion up, Corned Bee. I always wondered what symbols where. They are mentioned alot in my compilers but nowhere in any of my books (yet).