Say "Hello, World!" With C++

  • + 42 comments

    -Don't use 'printf' here, it's a C function. C++ is not C in any way. Use 'std::cout' instead.

    -Never use 'using namespace std'. This directive has been added for compatibility purpose and can lead into multiple definitions in larger projects. It breaks the namespace principle. However, you can use it for literals (e.g. 'using namespace std::chrono_literals'). If you really don't want to use the 'std::' prefix, you can use 'using std::cout', 'using std::endl' and so on for each function, class, etc. you want to import in the global namespace.

    #include <iostream>
    
    int main()
    {
        std::cout << "Hello, World!" << std::endl;
        return 0;
    }
    

    And with 'using':

    #include <iostream>
    
    using std::cout;
    using std::endl;
    
    int main()
    {
        cout << "Hello, World!" << endl;
        return 0;
    }