LKBEN10828: Howto write a first version of Hello World in C++ with a Microsoft visual Studio compiler


Symptom

You would like to start from the beginning and write your first program in C++.

Cause

none

Solution

To write a HelloWorld application with Microsoft Visual Studio you need to generate a new project. This project has to be generated as a Visual C++ Win32 Win32-console application. (do not use precompiled headers if you want to start from the beginning. Otherwise the following little program will not compile because it needs an extra line #include "stdafx.h")

You write the following in the HelloWorld.cpp file in the editor and start the compilation. (F7 starts the compiler, F5 will compile and run your program)



#include <iostream>
#include <ostream>

int main(void)
{
  ::std::cout << "Hello world!" << '\n';
}

Here you use the namespace std without a using statement first.
As an alternative you could write the following, which is exactly the same:


using namespace std;

int main(void)
{
  cout << "Hello world!" << '\n';
}

Here the namespace std will be used so the cout does not have to be prefixed with ::std to use it. I think this is easier to read than the previous example.

When this little program runs, you can take whichever C++ book from the shelf and start programming. Creating a GUI (grafical user interface) application is a bit more complex and not platform independant. (Here Java goes further than C/C++ by defining a standard GUI library wich C/C++ does not have)

Disclaimer:

The information provided in this document is intended for your information only. Lubby makes no claims to the validity of this information. Use of this information is at own risk!

About the Author

Author: Wim Peeters - Keskon GmbH & Co. KG

Wim Peeters is electronics engineer with an additional master in IT and over 30 years of experience, including time spent in support, development, consulting, training and database administration. Wim has worked with SQL Server since version 6.5. He has developed in C/C++, Java and C# on Windows and Linux. He writes knowledge base articles to solve IT problems and publishes them on the Lubby Knowledge Platform.

Latest update: 27-01-2023 | Comment: