Results 1 to 7 of 7

Thread: Newb Question: iostream and namespace std

  1. #1

    Thread Starter
    Hyperactive Member Comreak's Avatar
    Join Date
    Feb 2001
    Location
    Dis
    Posts
    319

    Newb Question: iostream and namespace std

    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.

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Hyperactive Member Comreak's Avatar
    Join Date
    Feb 2001
    Location
    Dis
    Posts
    319

    Post

    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 )?

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    So 'using namespace std' just says that your going to be using stuff from std.
    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.

    Another question, what are symbols?
    Symbol is the common name for variable, constant, function, class etc. names.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    Hyperactive Member Comreak's Avatar
    Join Date
    Feb 2001
    Location
    Dis
    Posts
    319
    So types = symbols?

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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
    Code:
    /* Define an array */
    int my_array[12]; // Array is 12 elements large
    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.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7

    Thread Starter
    Hyperactive Member Comreak's Avatar
    Join Date
    Feb 2001
    Location
    Dis
    Posts
    319
    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).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width