c++ definitions and explanations help

This is a discussion on c++ definitions and explanations help within the C++ Programming forums, part of the General Programming Boards category; the beginner tut. tells me some defintions and reasons of some stuff inside the code Code: #include <iostream> using namespace ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    10

    c++ definitions and explanations help

    the beginner tut. tells me some defintions and reasons of some stuff inside the code
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        cout<<"Hello,World!\n";
        cin.get();
        
    }
    But i was wondering what the std in namespace has to do w/ anything. i left it out and tried to compile it, but i got an error i didn't understand. so could someone tell me what the std is actually for and why it's needed? and maybe the difference between adding the code
    Code:
    return 1
    and not adding it? thanks!

  2. #2
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    std is the namespace used by STL (Standard Template Library) functions and data types. For example, cout is an example of a function of the std namespace. If you didn't specify the "using namespace std;", you would have to prefix "cout" with "std::" in order to make use of the function in the included header iostream.
    If you return 1 from main(), it indicates program failure. You need to return 0 to indicate successful program completion. Also, you need to put a semicolon after the return 0 statement.
    Last edited by Programmer_P; 06-25-2010 at 11:56 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,751
    Actually std::cout is an example of an object of a data type, the std:: ostream. In general, the types are what are standard, not their objects. But std::cout is special because of the purpose it serves and its name should conflict with no other cout object, thus it lives in the std namespace.

  4. #4
    Registered User
    Join Date
    Dec 2009
    Posts
    10
    ok so you guys are saying that the header called iostream contains many different namespaces, and that the name spaces themselves contain different functions? so it's like a cabinet with a bunch of folders and within those folders, opne of them has the "function" i want? oh, and how would the program know if it completed successfully or not? when i add in return 1 or return 0, i don't see any errors or anything. is there something wrong im doing?
    Last edited by yosimba2000; 06-26-2010 at 12:58 PM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,118
    Quote Originally Posted by Programmer_P
    std is the namespace used by STL (Standard Template Library) functions and data types.
    The C++ standard library, not the STL.

    Quote Originally Posted by yosimba2000
    ok so you guys are saying that the header called iostream contains many different namespaces, and that the name spaces themselves contain different functions?
    No, it contains the declarations for many different names, under the same std namespace. The names are names of functions and types, and in the case of std::cout, the name of a global object.

    There are namespaces within the std namespace, but you do not need to worry about them for now.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by laserlight View Post
    The C++ standard library, not the STL.
    I thought they both were the same. I guess not...
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,900
    Quote Originally Posted by yosimba2000 View Post
    oh, and how would the program know if it completed successfully or not? when i add in return 1 or return 0, i don't see any errors or anything. is there something wrong im doing?
    No, this is to be expected.
    The return values from main is what you want them to be and what you want them to mean. They don't actually mean anything to your program, but other programs which runs your program can pick up this value and see if your program executed correctly or not.
    The return value serves no other function.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,751
    And by "other programs which run your program" this includes the OS, and every OS I've heard of expects zero to mean success. Considering this is hello world, it amounts to answering "did it fail to print?"

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,900
    Most of the times, the OS doesn't care.
    I know Windows tends to pop up a dialog if a program doesn't run correctly (or if it thinks it doesn't), but I have no idea how this is implemented. I don't know if any other OS cares either?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,751
    >> I know Windows tends to pop up a dialog if a program doesn't run correctly (or if it thinks it doesn't), but I have no idea how this is implemented

    Then don't talk about it. I mean where do you expect return 0; from main to go? If it were truly pointless void main would be the rule and not the exception.

    Any OS that doesn't have a neutered shell also cares:

    $ proc && logout

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,900
    The point is that you're probably not going to see anything, regardless of what you return.
    The return value is not pointless. It can be used by other applications, just like the return value of a thread.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21