Useful Tips

How to create a "Hello World" console application in Visual C ++

As a development environment for Windows, the author advises using Visual Studio 2008 (800 MB) if you have Windows XP, Visual Studio 2013 (6.5 GB) for Windows 7 and above, or Dev C ++ 5 (41 MB) for those who have too much Slow internet to download Visual Studio. For users of Unix-like systems, you need a GCC compiler and any text editor such as Vim.

After you have installed everything you need, let's start writing the first program.

For Visual Studio

Open the menu “File → New → Project”. Go to the “General” tab and select “Empty project”. Give the project any name, for example “lesson1” and click “OK”.

In the Solution Explorer window (usually located in the upper left corner), right-click on the "source code files" folder. In the dialog box, select the menu item "Add → Create Item". Enter the name for the new file - main.cpp and click the "Add" button.

Create an empty file and open it with any text editor that has syntax highlighting.

Syntax Description

The #include directive is used to include other files in code. The line #include will be replaced by the contents of the file “iostream.h”, which is located in the standard language library and is responsible for inputting and outputting data to the screen.

#include connects the standard C language library. This connection is necessary for the system function to work.

The contents of the third line - using namespace std, indicates that we are using the default namespace called "std". Everything that is inside the curly braces of the int main () <> function will be automatically executed after the program starts.

The cout line tells the program to display a message with text "Hello world" to the screen.

The cout operator is used to display text on the command line screen. Two corner quotation marks () are placed after it. Next is the text to be displayed. It is placed in double quotes. The endl statement takes a line down.

If any failure occurs during execution, an error code other than zero will be generated. If the program operation ended without failures, then the error code will be zero. The return 0 command is necessary in order to send the operating system a message about the successful completion of the program.

- At the end of each team is placed semicolon.

Compilation and Launch

Now compile and run the program. Those who use MS Visual Studio need to press the key combination “Ctrl + F5”. GCC users need to run the following commands:

If the program was assembled the first time, then good. If the compiler says there are errors, then you did something wrong.

Read the error text and try to fix it on your own. If it does not work out, write about your problem in the comments.

As your homework, remake this program so that instead of the “Hello, World” message, the message “Hello, User” is displayed.

Creating and running a console application

Launch Visual Studio.

On the menu File select Create, Project.

A dialog box opens New project.

Expand Node Establishedexpand Patternsexpand Visual c #and then select Console application.

In field Name enter a name for the project and click OK.

AT solution explorer a new project will appear.

If the Program.cs file is not open in code editoropen the context menu Program.cs at solution explorerand then click View code.

Replaces the contents of the Program.cs file with the following code.

Press the F5 key to start the project. A command prompt window appears containing the string Hello World!

The following are important components of this program.

The first line contains a comment. The // characters convert the rest of the line into a comment.

You can also make a comment block of text, surrounding it with the characters / * and * /. This is shown in the following example.

The C # console application must contain the Main method, in which control begins and ends. The Main method creates objects and executes other methods.

The Main method is a static static method located inside a class or structure. In the previous example, "Hello World!" it is located in a class called Hello. The Main method can be declared in one of the following ways:

It returns a void value.

An integer value may also be returned.

With both return types, it can take the following arguments.

The Main args method parameter is an array of values ​​of type string that contains the command line arguments used to invoke the program. Unlike C ++, an array does not contain an executable (EXE) file name.

For more information about using command line arguments, see the examples in the Main () section and the command line arguments and How-to. Create and use assemblies using the command line.

Calling the ReadKey method at the end of the Main method prevents the console window from closing so that the output when running the program in debug mode can be read by pressing the F5 key.

C # programs typically use the I / O services provided by the runtime library in the .NET Framework. The System.Console.WriteLine statement ("Hello World!") Uses the WriteLine method. This is one of the output methods of the Console class in the runtime library. It displays its string parameters in a standard output stream, followed by a new line. Other Console methods are used for various input and output operations. If you put the using System, directive at the beginning of the program, you can use the System classes and methods directly without specifying their full name. For example, you can call Console.WriteLine instead of System.Console.WriteLine:

For more information on I / O methods, see System.IO.

Program "Hello World!" can be compiled using the command line rather than the Visual Studio integrated development environment (IDE).

Thank you for downloading Visual Studio and getting to know C ++!

  1. After starting Visual Studio 2015, familiarize yourself with the structure and views of the environment:
  2. Then study the standard build process of a Visual Studio project:
  3. Now start creating your first C ++ project by selecting a project template. If C ++ is not installed in Visual Studio, you must install Visual C ++ 2015 Tools for Windows Desktop:
  4. After installing Visual C ++ 2015 tools for Windows Desktop, you can select a template Win32 Console Application and create a project HelloWorld:
  5. Press button Doneto complete the job Win32 Application Wizards
  6. You will see your first C ++ project:
  7. Replace code following code.

  • Then add breakpointby clicking the gray area in front line 12:
  • Compile and run the project by clicking green triangle on the toolbar (Windows Local Debugger) or by pressing the F5 key. In Visual Studio, the build and debug process starts with one click. Click Yes to build the project:

  • “Hello World!” Appears in console window.
  • If the console window closes immediately, you need to set a breakpoint in step 8.

    If you are using Windows 8.1, you must restart the Visual Studio installer, click the "Change" button and select C ++ from the list of languages.

    Congratulations on creating your first C ++ project! Please report if this page was helpful. Thank.