Chuyển tới nội dung
Home » Hello World C Visual Studio Code | Open Folder

Hello World C Visual Studio Code | Open Folder

How to print Hello World  using Visual Studio code | C programming

Prerequisites

To successfully complete this tutorial, you must do the following:

  1. Install Visual Studio Code.

  2. Install the C++ extension for VS Code. You can install the C/C++ extension by searching for ‘c++’ in the Extensions view (⇧⌘X (Windows, Linux Ctrl+Shift+X)).

Ensure GCC is installed

Although you’ll use VS Code to edit your source code, you’ll compile the source code on Linux using the g++ compiler. You’ll also use GDB to debug. These tools are not installed by default on Ubuntu, so you have to install them. Fortunately, that’s easy.

First, check to see whether GCC is already installed. To verify whether it is, open a Terminal window and enter the following command:


gcc -v

If GCC isn’t installed, run the following command from the terminal window to update the Ubuntu package lists. An out-of-date Linux distribution can sometimes interfere with attempts to install new packages.


sudo apt-get update

Next install the GNU compiler tools and the GDB debugger with this command:


sudo apt-get install build-essential gdb

Customize debugging with launch.json

When you debug with the play button or F5, the C++ extension creates a dynamic debug configuration on the fly.

There are cases where you’d want to customize your debug configuration, such as specifying arguments to pass to the program at runtime. You can define custom debug configurations in a

launch.json

file.

To create

launch.json

, choose Add Debug Configuration from the play button drop-down menu.

You’ll then see a dropdown for various predefined debugging configurations. Choose g++ build and debug active file.

VS Code creates a

launch.json

file, which looks something like this:


{ "version": "0.2.0", "configurations": [ { "name": "C/C++: g++ build and debug active file", "type": "cppdbg", "request": "launch", "program": "${fileDirname}/${fileBasenameNoExtension}", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": false, "MIMode": "gdb", "miDebuggerPath": "/usr/bin/gdb", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "preLaunchTask": "C/C++: g++ build active file" } ] }

In the JSON above,

program

specifies the program you want to debug. Here it is set to the active file folder

${fileDirname}

and active filename without an extension

${fileBasenameNoExtension}

, which if

helloworld.cpp

is the active file will be

helloworld

. The

args

property is an array of arguments to pass to the program at runtime.

By default, the C++ extension won’t add any breakpoints to your source code and the

stopAtEntry

value is set to

false

.

Change the

stopAtEntry

value to

true

to cause the debugger to stop on the

main

method when you start debugging.

From now on, the play button and F5 will read from your


launch.json

file when launching your program for debugging.

How to print Hello World  using Visual Studio code | C programming
How to print Hello World using Visual Studio code | C programming

Set up your C++ Environment

C++ is a compiled language meaning your program’s source code must be translated (compiled) before it can be run on your computer. The C/C++ extension doesn’t include a C++ compiler or debugger, since VS Code as an editor relies on command-line tools for the development workflow. You need to install these tools or use the tools already installed on your computer.

Check if you have a compiler installed

Note: There may already be a C++ compiler and debugger provided by your academic or work development environment. Check with your instructors or colleagues for guidance on installing the recommended C++ toolset (compiler, debugger, project system, linter).

Common compilers that already come preinstalled on some platforms are the GNU Compiler Collection (GCC) on Linux and the Clang tools with Xcode on macOS.

To check if you already have them installed:

  1. Open a new VS Code terminal window using (⌃⇧` (Windows, Linux Ctrl+Shift+`))

  2. Use the following command to check for the GCC compiler


    g++

    :

    g++ --version

    Or this command for the Clang compiler


    clang

    :

    clang --version

The output should show you the compiler version and details. If neither are found, make sure your compiler executable is in your platform path (

%PATH

on Windows,

$PATH

on Linux and macOS) so that the C/C++ extension can find it. Otherwise, use the instructions in the section below to install a compiler.

Install a compiler

If you don’t have a compiler installed, you can follow one of our installation tutorials:

Windows:

Linux:

macOS:

Note: If you would prefer a full Integrated Development Environment (IDE), with built-in compilation, debugging, and project templates (File > New Project), there are many options available, such as the Visual Studio Community edition.

Remote Development

VS Code and the C++ extension support Remote Development allowing you to work over SSH on a remote machine or VM, inside a Docker container, or in the Windows Subsystem for Linux (WSL).

To install support for Remote Development:

  1. Install the VS Code Remote Development Extension Pack.
  2. If the remote source files are hosted in WSL, use the WSL extension.
  3. If you are connecting to a remote machine with SSH, use the Remote – SSH extension.
  4. If the remote source files are hosted in a container (for example, Docker), use the Dev Containers extension.
Cách code c/c++ bài 1: Hello World trên visual studio code.
Cách code c/c++ bài 1: Hello World trên visual studio code.

Explore the debugger

Before you start stepping through the code, let’s take a moment to notice several changes in the user interface:

  • The Integrated Terminal appears at the bottom of the source code editor. In the Debug Output tab, you see output that indicates the debugger is up and running.

  • The editor highlights line 12, which is a breakpoint that you set before starting the debugger:

  • The Run and Debug view on the left shows debugging information. You’ll see an example later in the tutorial.

  • At the top of the code editor, a debugging control panel appears. You can move this around the screen by grabbing the dots on the left side.

If you already have a launch.json file in your workspace, the play button will read from it when figuring out how run and debug your C++ file. If you don’t have launch.json, the play button will create a temporary “quick debug” configuration on the fly, eliminating the need for launch.json altogether!

Run helloworld.cpp

  1. Make sure you have


    helloworld.cpp

    open so it is the active file in your editor.

  2. Press the play button in the top right corner of the editor.

  3. Choose C/C++: g++.exe build and debug active file from the list of detected compilers on your system.

You are only prompted to choose a compiler the first time you run

helloworld.cpp

. This compiler becomes “default” compiler set in your

tasks.json

file.

  1. After the build succeeds, you should see “Hello World” appear in the integrated Terminal.

Congratulations! You’ve just run your first C++ program in VS Code! The next step is to learn more about the Microsoft C/C++ extension’s language features such as IntelliSense, code navigation, build configuration, and debugging using one of the Tutorials in the next section.

How to Set up Visual Studio Code for C and C++ Programming
How to Set up Visual Studio Code for C and C++ Programming

C/C++ for Visual Studio Code

C/C++ support for Visual Studio Code is provided by a Microsoft C/C++ extension to enable cross-platform C and C++ development on Windows, Linux, and macOS. When you create a

*.cpp

file, the extension adds features such as syntax highlighting (colorization), smart completions and hovers (IntelliSense), and error checking.

Next steps

  • Explore the VS Code User Guide.
  • Review the Overview of the C++ extension.
  • Create a new workspace, copy your .json files to it, adjust the necessary settings for the new workspace path, program name, and so on, and start coding!

This example introduces you to the basic functionality of VS Code by demonstrating how to write a “hello world” program in C++. Before continuing, make sure you have the “ms-vscode.cpptools” extension installed.

Initialize the Project

The first step is to create a new project. To do this, load the VS Code program. You should be greeted with the typical welcome screen:

To create the first program, select “Start” > “New file” from the welcome screen. This will open a new file window. Go ahead and save the file (“File” > “Save”) into a new directory. You can name the directory anything you want, but this example will call the directory “VSC_HelloWorld” and the file “HelloWorld.cpp”.

Now write the actual program (feel free to copy the below text):


#include

int main() { // Output the hello world text std::cout << "Hello world!" << std::endl; return 0; }

Great! You’ll also notice that because you’ve installed the “ms-vscode.cpptools” extension you also have pretty code-highlighting. Now let’s move on to running the code.

Running the Script (basic)

We can run “HelloWorld.cpp” from within VS Code itself. The simplest way to run such a program is to open the integrated terminal (“View” > “Integrated Terminal”). This opens a terminal window in the lower portion of the view. From inside this terminal we can navigate to our created directory, build, and execute the script we’ve written.

Here we’ve used the following commands to compile and run the code:


$ g++ HelloWorld.cpp -o hellowold $ ./hellowold

Notice that we get the expected

Hello World!

output.

Running the Script (slightly more advanced)

Great, but we can use VS Code directly to build and execute the code as well. For this, we first need to turn the “VSC_HelloWorld” directory into a workspace. This can be done by:

The Explorer menu now displays the contents of the directory.

Next we want to define the actual tasks which we want VS Code to run. To do this, select “Tasks” > “Configure Default Build Task”. In the drop down menu, select “Other”. This opens a new file called “tasks.json” which contains some default values for a task. We need to change these values. Update this file to contain the following and save it:


{ "version": "2.0.0", "tasks": [ { "taskName": "build", "type": "shell", "command": "g++ HelloWorld.cpp -o helloworld" }, { "taskName": "run", "type": "shell", "command": "${workspaceRoot}/helloworld" } ] }

Note that the above also creates a hidden .vscode directory within our working directory. This is where VS Code puts configuration files including any project specific settings files. You can find out more about Tasks here.

In the above example,

${workspaceRoot}

references the top level directory of our workspace, which is our “VSC_HelloWorld” directory. Now, to build the project from inside the method select “Tasks” > “Run Build Task…” and select our created “build” task and “Continue without scanning the task output” from the drop down menus that show up. Then we can run the executable using “Tasks” > “Run Task…” and selecting the “run” task we created. If you have the integrated terminal open, you’ll notice that the “Hello World!” text will be printed there.

It is possible that the terminal may close before you are able to view the output. If this happens you can insert a line of code like this

int i; std::cin >> i;

just before the return statement at the end of the

main()

function. You can then end the script by typing any number and pressing

And that’s it! You can now start writing and running your C++ scripts from within VS Code.

How to Create and Run C Program in Visual Studio
How to Create and Run C Program in Visual Studio

Step through the code

Now you’re ready to start stepping through the code.

  1. Click or press the Step over icon in the debugging control panel.

    This will advance program execution to the first line of the for loop, and skip over all the internal function calls within the


    vector

    and

    string

    classes that are invoked when the

    msg

    variable is created and initialized. Notice the change in the Variables window on the side.

  2. Press Step over again to advance to the next statement in this program (skipping over all the internal code that is executed to initialize the loop). Now, the Variables window shows information about the loop variables.

  3. Press Step over again to execute the


    cout

    statement. (Note that the C++ extension does not print any output to the Debug Console until the last cout executes.)

  4. If you like, you can keep pressing Step over until all the words in the vector have been printed to the console. But if you are curious, try pressing the Step Into button to step through source code in the C++ standard library!

    To return to your own code, one way is to keep pressing Step over. Another way is to set a breakpoint in your code by switching to the


    helloworld.cpp

    tab in the code editor, putting the insertion point somewhere on the

    cout

    statement inside the loop, and pressing F9. A red dot appears in the gutter on the left to indicate that a breakpoint has been set on this line.

    Then press F5 to start execution from the current line in the standard library header. Execution will break on


    cout

    . If you like, you can press F9 again to toggle off the breakpoint.

    When the loop has completed, you can see the output in the Debug Console tab of the integrated terminal, along with some other diagnostic information that is output by GDB.

Set a watch

To keep track of the value of a variable as your program executes, set a watch on the variable.

  1. Place the insertion point inside the loop. In the Watch window, click the plus sign and in the text box, type


    word

    , which is the name of the loop variable. Now view the Watch window as you step through the loop.

  2. To quickly view the value of any variable while execution is paused on a breakpoint, you can hover over it with the mouse pointer.

Next, you’ll create a

tasks.json

file to tell VS Code how to build (compile) the program. This task will invoke the g++ compiler to create an executable file from the source code.

It’s important to have

helloworld.cpp

open in the editor because the next step uses the active file in the editor for context to create the build task in the next step.

Install VSCode and run C# Hello World
Install VSCode and run C# Hello World

Example: Install MinGW-x64 on Windows

To understand the process, let’s install Mingw-w64 via MSYS2. Mingw-w64 is a popular, free toolset on Windows. It provides up-to-date native builds of GCC, Mingw-w64, and other helpful C++ tools and libraries.

  1. Download using this direct link to the MinGW installer.

  2. Run the installer and follow the steps of the installation wizard. Note, MSYS2 requires 64 bit Windows 8.1 or newer.

  3. In the wizard, choose your desired Installation Folder. Record this directory for later. In most cases, the recommended directory is acceptable. The same applies when you get to setting the start menu shortcuts step. When complete, ensure the Run MSYS2 now box is checked and select Finish. A MSYS2 terminal window will then automatically open.

  4. In this terminal, install the MinGW-w64 toolchain by running the following command:


    pacman -S --needed base-devel mingw-w64-ucrt-x86_64-toolchain

  5. Accept the default number of packages in the


    toolchain

    group by pressing Enter.

  6. Enter

    when prompted whether to proceed with the installation.

  7. Add the path to your MinGW-w64


    bin

    folder to the Windows

    PATH

    environment variable by using the following steps:

    1. In the Windows search bar, type Settings to open your Windows Settings.
    2. Search for Edit environment variables for your account.
    3. In your User variables, select the

      Path

      variable and then select Edit.
    4. Select New and add the MinGW-w64 destination folder you recorded during the installation process to the list. If you selected the default installation steps, the path is:

      C:\msys64\ucrt64\bin

      .
    5. Select OK to save the updated PATH. For the new

      PATH

      to be available, reopen your console windows.
  8. Check that your MinGW-w64 tools are correctly installed and available, open a new Command Prompt and type:


gcc --version g++ --version gdb --version

You should see output that states which versions of GCC, g++ and GDB you have installed. If this is not the case, make sure your PATH entry matches the Mingw-w64 binary location where the compiler tools are located or reference the troubleshooting section.

Open folder

By starting VS Code in a folder, that folder becomes your “workspace”. VS Code stores settings that are specific to that workspace in

.vscode/settings.json

, which are separate from user settings that are stored globally.

Using a terminal, create an empty folder called “hello”, navigate into it, and open VS Code (code) in that folder (.) by entering the following commands:


mkdir hello cd hello code .

Alternatively, you can run VS Code through the operating system UI, then use File > Open Folder to open the project folder.

Hello World in Java using Visual Studio Code
Hello World in Java using Visual Studio Code

Explore IntelliSense

In the

helloworld.cpp

file, hover over

vector

or

string

to see type information. After the declaration of the

msg

variable, start typing

msg.

as you would when calling a member function. You should immediately see a completion list that shows all the member functions, and a window that shows the type information for the

msg

object:

You can press the Tab key to insert the selected member. Then, when you add the opening parenthesis, you’ll see information about arguments that the function requires.

Debug helloworld.cpp

To debug your code,

  1. Go back to

    helloworld.cpp

    so that it is the active file.
  2. Set a breakpoint by clicking on the editor margin or using F9 on the current line.
  3. From the drop-down next to the play button, select Debug C/C++ File.
  4. Choose C/C++: g++ build and debug active file from the list of detected compilers on your system (you’ll only be asked to choose a compiler the first time you run or debug

    helloworld.cpp

    ).

The play button has two modes: Run C/C++ File and Debug C/C++ File. It will default to the last-used mode. If you see the debug icon in the play button, you can just select the play button to debug, instead of selecting the drop-down menu item.

C++ Hello World in Visual Studio Code
C++ Hello World in Visual Studio Code

Join the community

Find community resources and connect with user groups.

.NET developer community – Meet with like-minded developers

Using C++ on Linux in VS Code

In this tutorial, you will configure Visual Studio Code to use the GCC C++ compiler (g++) and GDB debugger on Linux. GCC stands for GNU Compiler Collection; GDB is the GNU debugger.

After configuring VS Code, you will compile and debug a simple C++ program in VS Code. This tutorial does not teach you GCC, GDB, Ubuntu or the C++ language. For those subjects, there are many good resources available on the Web.

If you have trouble, feel free to file an issue for this tutorial in the VS Code documentation repository.

Troubleshooting

Compiler and linking errors

The most common cause of errors (such as

undefined _main

, or

attempting to link with file built for unknown-unsupported file format

, and so on) occurs when

helloworld.cpp

is not the active file when you start a build or start debugging. This is because the compiler is trying to compile something that isn’t source code, like your

launch.json

,

tasks.json

, or

c_cpp_properties.json

file.

TYPESCRIPT HELLO WORLD ASSIGNMENT1 (GOVERNOR SINDH IT COURSE
TYPESCRIPT HELLO WORLD ASSIGNMENT1 (GOVERNOR SINDH IT COURSE

Install

Installing VS Code and extensions

  1. If you haven’t already done so, install VS Code.
  2. Next, install C# Dev Kit from the Visual Studio Marketplace. For additional details on installing extensions, read Extension Marketplace. The C# extension is called C# Dev Kit and it’s published by Microsoft.

Note: C# Dev Kit supports cloud native development. To do cross-platform mobile and desktop development, you can use C# Dev Kit with the .NET MAUI extension. Learn how to get set up with .NET MAUI in VS Code.

Upon installation, C# Dev Kit launches an extension walkthrough. You can follow the steps of this walkthrough to learn more about the features of the C# extension. Reopen the walkthrough at any time by opening the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)) and selecting Welcome: Open Walkthrough. Here, select Get Started with C# Dev Kit.

Note: You are required to sign in to a Visual Studio subscription to use C# Dev Kit. Check out the Signing in to C# Dev Kit documentation to learn more.

Installing the .NET Coding Pack for students

If you’re a student, we recommend installing the .NET Coding Pack for an easier setup experience. The Coding Pack includes VS Code, the .NET SDK, and essential .NET extensions. The Coding Pack can be used as a clean installation, or to update or repair an existing development environment.

Install the .NET Coding Pack – Windows

Install the .NET Coding Pack – macOS

Note: The .NET Coding Pack is only available for Windows and macOS. For other operating systems, you need to manually install the .NET SDK, VS Code, and .NET extensions.

Create a Hello World app

First, ensure you are within the new folder (workspace) that you created. From here, you can create the project in two ways.

Use the Command Palette

  1. Bring up the Command Palette using ⇧⌘P (Windows, Linux Ctrl+Shift+P) and then type “.NET”.
  2. Find and select the .NET: New Project command.
  3. After selecting the command, you’ll need to choose the project template. Choose Console app.
  4. To run your app, select Run > Start Debugging in the upper menu, or use the F5 keyboard shortcut. To learn more about debugging your C# project, read the debugging documentation.

Use the terminal

  1. Open a terminal/command prompt and navigate to the folder in which you’d like to create the app. Enter the following command in the command shell:


    dotnet new console

  2. When the project folder is first opened in VS Code:

    A “Required assets to build and debug are missing. Add them?” notification appears at the bottom right of the window.

    Select Yes.

  3. Run the app by entering the following command in the command shell:


    dotnet run

Learn Visual Studio 2022 in 45 minutes | Amit Thinks
Learn Visual Studio 2022 in 45 minutes | Amit Thinks

Enhance completions with AI

GitHub Copilot is an AI-powered code completion tool that helps you write code faster and smarter. You can use the GitHub Copilot extension in VS Code to generate code, or to learn from the code it generates.

GitHub Copilot provides suggestions for numerous languages and a wide variety of frameworks, and it works especially well for Python, JavaScript, TypeScript, Ruby, Go, C# and C++.

You can learn more about how to get started with Copilot in the Copilot documentation.

Create Hello World

From the terminal window, create an empty folder called

projects

to store your VS Code projects. Then create a subfolder called

helloworld

, navigate into it, and open VS Code in that folder by entering the following commands:


mkdir projects cd projects mkdir helloworld cd helloworld code .

The

code .

command opens VS Code in the current working folder, which becomes your “workspace”. As you go through the tutorial, you will create three files in a

.vscode

folder in the workspace:


  • tasks.json

    (compiler build settings)

  • launch.json

    (debugger settings)

  • c_cpp_properties.json

    (compiler path and IntelliSense settings)

Add hello world source code file

In the File Explorer title bar, select New File and name the file

helloworld.cpp

.

Paste in the following source code:


#include

#include

#include

using namespace std; int main() { vector

msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"}; for (const string& word : msg) { cout << word << " "; } cout << endl; }




Now press ⌘S (Windows, Linux Ctrl+S) to save the file. Notice that your files are listed in the File Explorer view (⇧⌘E (Windows, Linux Ctrl+Shift+E)) in the side bar of VS Code:

You can also enable Auto Save to automatically save your file changes, by checking Auto Save in the main File menu.

The Activity Bar on the edge of Visual Studio Code lets you open different views such as Search, Source Control, and Run. You’ll look at the Run view later in this tutorial. You can find out more about the other views in the VS Code User Interface documentation.

Note: When you save or open a C++ file, you may see a notification from the C/C++ extension about the availability of an Insiders version, which lets you test new features and fixes. You can ignore this notification by selecting the

(Clear Notification).

How to Run C# using Visual Studio Code | VSCode Setup for C# Programming
How to Run C# using Visual Studio Code | VSCode Setup for C# Programming

Run helloworld.cpp

Remember, the C++ extension uses the C++ compiler you have installed on your machine to build your program. Make sure you have a C++ compiler installed before attempting to run and debug

helloworld.cpp

in VS Code.

  1. Open


    helloworld.cpp

    so that it is the active file.

  2. Press the play button in the top right corner of the editor.

  3. Choose g++ build and debug active file from the list of detected compilers on your system.

You’ll only be asked to choose a compiler the first time you run

helloworld.cpp

. This compiler will be set as the “default” compiler in

tasks.json

file.

  1. After the build succeeds, your program’s output will appear in the integrated Terminal.

The first time you run your program, the C++ extension creates

tasks.json

, which you’ll find in your project’s

.vscode

folder.

tasks.json

stores build configurations.

Your new

tasks.json

file should look similar to the JSON below:


{ "version": "2.0.0", "tasks": [ { "type": "shell", "label": "C/C++: g++ build active file", "command": "/usr/bin/g++", "args": ["-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}"], "options": { "cwd": "/usr/bin" }, "problemMatcher": ["$gcc"], "group": { "kind": "build", "isDefault": true }, "detail": "Task generated by Debugger." } ] }

Note: You can learn more about


tasks.json

variables in the variables reference.

The

command

setting specifies the program to run; in this case that is g++.
The

args

array specifies the command-line arguments that will be passed to g++. These arguments must be specified in the order expected by the compiler.

This task tells g++ to take the active file (

${file}

), compile it, and create an executable file in the current directory (

${fileDirname}

) with the same name as the active file but without an extension (

${fileBasenameNoExtension}

), resulting in

helloworld

for our example.

The

label

value is what you will see in the tasks list; you can name this whatever you like.

The

detail

value is what you will as the description of the task in the tasks list. It’s highly recommended to rename this value to differentiate it from similar tasks.

From now on, the play button will read from

tasks.json

to figure out how to build and run your program. You can define multiple build tasks in

tasks.json

, and whichever task is marked as the default will be used by the play button. In case you need to change the default compiler, you can run Tasks: Configure default build task. Alternatively you can modify the

tasks.json

file and remove the default by replacing this segment:


"group": { "kind": "build", "isDefault": true },

with this:


"group": "build",

Modifying tasks.json

You can modify your

tasks.json

to build multiple C++ files by using an argument like

"${workspaceFolder}/*.cpp"

instead of

"${file}"

.This will build all

.cpp

files in your current folder. You can also modify the output filename by replacing

"${fileDirname}/${fileBasenameNoExtension}"

with a hard-coded filename (for example ‘helloworld.out’).

Feedback

If you run into any issues or have suggestions for the Microsoft C/C++ extension, please file issues and suggestions on GitHub. If you haven’t already provided feedback, you can take this quick survey.

It is time to create your C hello world program. By tradition, a “Hello, world!” program just prints that greeting on the screen. This is usually the first program that any developer creates. It serves two purposes:

Advertise on this site. I promise you will like the rates 🙂

In this lesson, I will show you how to create your first program for each of the environments from the previous lesson. You will pick and create the program for the IDE that you chose. To create a program you will:

No matter which environment you use, the code for this example is the same:

1 2 3 4 5 6 7

#include

int main() { printf(“Hello world!\n”); return 0; }

I will show you how to write compile and run the code in the IDEs that we looked at in the last lesson:

I am using Visual Studio Community 2015, but the process is very similar to previous versions(both Express and Professional editions). Every program in Visual Studio is created within a project. To create your C hello world program, create a new project:

1. Open Visual Studio and go to File->New->Project

If this is a new installation, it is possible that the template component is not installed yet. If that is the case, double click on the “Install Visual C++” text to add the component.

You have just created a new empty project. You can see its contents in the “Solution Explorer”. The solution explorer is located to the right, by default. To make it your C hello world project:

Now you are ready to run your C Hello World program!

The result looks like this:

To compile and run code you need a compiler. Make sure you have the Code::Blocks distribution that includes the GCC, otherwise you need to install a compiler manually.

Code::Blocks allows you to create a program in several ways.

The first two methods are convenient only if your program consists of just 1-2-3 files. If your program has more files it is preferred to put them in a project, because it is easier to navigate.

With the C hello world we want to know that everything is working as expected, so let’s create your first project! Start Code::Blocks and from nemu File create a new porject:

Now in the left side of the screen go to Projects in the “Management” sub-window. Expand your new project and its folder “Sources”. You will see that Code::Blocks automatically added a new source file “main.c”. Double click it, to open it.

You see? The “hello-world” code is already there 😉

It is time to compile and run your first C program. Go to menu Build->”Build and Run” or just press F9 and then OK, when you are asked if you want to build the project.

If you downloaded a distribution without a compiler, you need to install one manually.

There are two ways to create our “C hello world” program in Dev-C++:

For a this short program it is easier to choose the first option. However, later for programs with more files it is better to use a project. Here is how to create a C project in Dev Cpp:

In the next window just click “Save” to save the project files in the default location.

Now your project is created and it contains one source file:

Before you can compile and run your C hello world, you need to install a C compiler. For instance to install the GCC in any Debian based distribution, for instance Ubuntu, do the following:

In Geany you can:

I will show you the second option:

This will generate a new C source file. It will start with a 20 row comment(the red text starting with /* and ending with */) that you can delete right away.

Add the lineprintf(“Hello, world!\n”);between the opening curly bracket “{” and the line “return 0;”.

Now you just need to build and execute the file:

When the build is done, you will see the message “Compilation finished successfully.” in the Compiler section below the code.

Now press F5 or the “Execute” button to start the program. You will see a console that displays the “Hello, world!” string.

You have just created your C Hello World program! Your development environment is working and tested, so you are ready to continue with the actual part of the basic C tutorial.

In the next lesson you will see what is the typical structure of a C program. To explain it, I will use the example from this lesson.

Previous lesson: C programming software

Next lesson: C Language Program

  1. GIỚI THIỆU VISUAL STUDIO CODE (VS CODE):

VSCode là một công cụ lâp trình (Code Editor) do Microsoft phát triển, VSCode có thể cài đặt và sử dụng trên cả Windows, MacOS và Linux. VSCode là hoàn toàn miễn phí.

VSCode là sự kết hợp giữa tính đơn giản của một trình biên tập code (code Editor) và các công cụ hỗ trợ (Extensions) mạnh mẽ dành cho lập trình viên như: Debugger, Git, Terminal, và còn nhiều hơn nữa.

  1. CÀI ĐẶT VISUAL STUDIO CODE:

CÀI ĐẶT VSCODE CHO WINDOWS 10 và MAC:

– Vào trang download của VSCode theo địa chỉ: https://code.visualstudio.com/Download

– Trên trang download chỉ đơn giản chọn file cần tải cho từng hệ điều hành đang sử dụng.

2.1. Đối với các máy MAC chỉ có 1 lựa chọn duy nhất là file .zip.

2.2. Còn đối với máy dùng hệ điều hành windows chúng ta có 3 lựa chọn file tải xuống.

2.2.1. Chọn file .zip:

– Đối với file .zip chúng ta không cần phải cài đặt. Chỉ việc download file .zip về và bung file (unzip) là có thể sử dụng ngay.

  • Sau khi lưu file về máy (sử dụng bất kỳ 1 công cụ giải nén nào như: Winrar, winzip, hoặc 7zip… để giải nén file), nhấp chuột phải vào file chọn Extract Here để giải nén và sử dụng.
  • Giờ chúng ta có thể sử dụng visual Studio Code mà không cần phải cài đặt.

2.2.2. Chọn tải file “User Installer” và “System Installer” : Trường hợp này chúng ta thực hiện chọn “Next” theo các bước hướng dẫn cài đặt của Installer.

  1. CÀI ĐẶT CÁC EXTENSION “C/C++” VÀ “Code Runner”:

Để thực hiện code C/C++ trên VSCode chúng ta cần cài thêm 1 số các công cụ mở rộng (Extensions) là “C/C++” và “Code Runner”.

3.1. CÀI ĐẶT EXTENSIONS C/C++:

– Trên công cụ VSCode đang được mở, chọn tab extensions phía bên trái hoặc tổ hợp phím Ctrl+ shift + X, extension Marketplace sẽ được mở ra.

– Trên Marketplace trong phần Search Extensions, gõ C/C++ để tìm kiếm công cụ

  • Chọn Install đề cài đặt extensions, hãy cài đặt lựa chọn này vì nó được Microsoft viết ra. Công cụ này giúp chúng ta viết code, kiểm tra các cú pháp, và đặc biệt có thể đưa cho chúng ta các gợi ý code giúp việc lập trình trở nên dễ dàng hơn so với công cụ DEV C/C++ trước đây.

3.2. CÀI ĐẶT EXTENSIONS CODE RUNNER:

Ngoài ra chúng ta cần cài thêm 1 extensions khác là “Code Runner”, extensions này sẽ giúp chúng ta biên dịch và thực thi các source code C/C++

3.3. CÀI ĐẶT BIẾN MÔI TRƯỜNG:

Sau khi đã cài đặt thành công 2 extensions, việc tiếp theo của chúng ta là cài đặt biến môi trường cho việc gọi các thư viện và thực thi code.

3.3.1. Tải thư viện:

– Để cài đặt môi trường thực thi ta cần downlaod thư viện tại: https://sourceforge.net/projects/mingw-w64/

chờ khoảng 5 giây, trình biên dịch sẽ tự tải xuống. Sau đó, các bạn hãy tiến hành cài đặt.

– Hoặc có thể search tên file “mingw-w64-install.exe” trên công cụ tìm kiếm của google.

– Sau khi tải được file “mingw-w64-install.exe” tiến hành cài đặt. Chọn “Next” đề cài đặt như bình thường.

– Lưu lại đường dẫn trong quá trình cài đặt, thường mặc định đường dẫn sẽ là: C:\Program Files (x86)\mingw-w64\i686-8.1.0-posix-dwarf-rt_v6-rev0

3.3.2. Thiết lập biến môi trường (Enviroment Variables):

– Để thiết lập biến môi trường, cần mở của số System bằng cách kích Chuột phải vào biểu tượng This PC -> Properties. Hoặc vào Control Panel -> System and Security -> System

– Chọn Advanced system settings -> Environment Variables.

– Trong mục System variables, chọn Path -> Edit.

– Chọn New\ Tìm đường dẫn chứa thư mục bin của mingw-w64 vừa cài đặt (mặc định sẽ là C:\Program Files (x86)\mingw-w64\i686-8.1.0-posix-dwarf-rt_v6-rev0\mingw32\bin)

– Copy đường dẫn này, paste vào rồi nhấn OK.

– Khởi động lại máy tính để đảm bảo hệ điều hành cập nhật thông tin biến môi trường vừa thêm.

  1. LẬP TRÌNH C/C++ TRÊN VSCODE:

Sau khi đã cài đặt thành công 2 extensions C/C++, Code Runner và thiết đặt biến môi trường, chúng ta sẽ bắt đầu tạo file và gõ những dòng code đầu tiên của lập trình C/C++.

– Vào menu File/ chọn New File..

– Chương trình sẽ tạo cho chúng ta 1 file mới, ở đây hãy kích chọn “Select a Language” để lựa chọn ngôn ngữ lập trình mà chúng ta sử dụng là C(c) hoặc C++(cpp).

– Sau đó nhấn tổ hợp phím Ctrl + S hoặc vào menu File/ chọn Save… để lưu lại file chúng ta vừa tạo.

(lưu ý: trong đường dẫn lưu file thì không nên sử dụng tiếng việt có dấu và không có các ký tự đặt biệt).

4.1. BÀI HỌC ĐẦU TIÊN:

– Hãy bắt đầu thử chương trình đầu tiên bằng Hello World, Raghu Venkatesh đã nói: “If you can write “hello world” you can change the world”. Vậy nên muốn thay đổi thế giới hãy bắt đầu bằng Hello World nhé!

– Khi code chức năng IntelliSense của VSCode sẽ cho chúng ta các gợi ý code, hãy chọn thôi:

– Sau khi in ra dòng chữ “Hello World” đầu tiên, hãy nhấp chuột phải chọn “Run Code” để xem kết quả hiển thị dưới tab OUTPUT:

4.2. RUN CODE TRÊN TERMINAL:

– Bài tiếp theo chúng ta sẽ viết 1 chương trình cộng/Trừ 2 số nguyên, mà giá trị của 2 số sẽ được nhập vào từ bàn phím.

– Hãy tạo 1 file mới có nội dung như sau:

– Sau khi hoàn thành, chúng ta không thể chọn Run Code để thực thi hiển thị dưới tab Output, vì tab OUTPUT chỉ hỗ trợ hiển thị các thông tin được in ra màn hình. Trong bài này chúng ta thực hiện cho người dùng nhập giá trị từ bàn phím vào nên không thể hiển thị trong tab OUTPUT.

– Chúng ta sẽ thực hiện cấu hình cho chương trình thực thi và hiển thị trong tab TERMINAL

4.2.1. Đối với hệ điều hành Windows: vào menu File/ References/ chọn Settings.

4.2.2. Đối với hệ điều hành MAC: vào menu Code/ References/ chọn Settings.

– Trong tab Setting được mở ra, Gõ “Code Runner” trên thanh tìm kiếm để tìm extensions Code Runner. Kéo xuống dưới, tìm và chọn Code-runner: Run In Terminal.

– Sau đó tắt VSCode, khởi động lại để hệ thống cập nhật các thay đổi vừa chọn.

– Chạy lại chương trình, chúng ta sẽ có kết quả hiển thị trong tab Terminal

  1. TỔNG KẾT:

– Chúng ta đã hoàn thành xong phần cài đặt công cụ VSCode cho lập trình C/C++, VSCode còn khá nhiều các extensions khác hữu ích cho công việc lập trình mà chúng ta có thể tham khảo thêm trên trang của Microsoft tại địa chỉ : https://code.visualstudio.com/docs/languages/

– Các bạn có thể tìm và cài đặt thêm extensions hữu ích khác cho môn học HTML/CSS tiếp theo ở đây: https://code.visualstudio.com/docs/languages/css

Chúc các bạn thành công.

Getting Started with C# in VS Code

This getting started guide introduces you to C# and .NET for Visual Studio Code through the following tasks:

  1. Installing and setting up your VS Code environment for C#.
  2. Writing and running a simple “Hello World” application using C#.
  3. Introduce you to other learning resources for C# in VS Code.

Keep in mind, that this guide won’t teach you C#. Instead, it teaches you how to get set up for C# development in VS Code. If you’re looking for resources to learn C#, check out our C# curriculum.

How to set up C++ in Visual Studio Code
How to set up C++ in Visual Studio Code

Create a Hello World App

To make sure the compiler is installed and configured correctly, lets create a Hello World C++ program.

Create a C++ file

  1. On Windows, launch a Windows command prompt (Enter Windows command prompt in the Windows search bar). On macOS and Linux, you can enter these commands in the terminal.
  2. Run the following commands. They are creating an empty folder called

    projects

    where you can place all your VS Code projects. The next commands create and navigate you to a subfolder called

    helloworld

    . From there, you are opening

    helloworld

    directly in VS Code using the

    code

    command.


mkdir projects cd projects mkdir helloworld cd helloworld code .

The “code .” command opens VS Code in the current working folder, which becomes your “workspace”. Accept the Workspace Trust dialog by selecting Yes, I trust the authors since this is a folder you created.

Now create a new file called

helloworld.cpp

with the New File button in the File Explorer or File > New File command.

Add Hello World source code

Paste in the following source code:


#include

int main() { std::cout << "Hello World" << std::endl; }

Now press ⌘S (Windows, Linux Ctrl+S) to save the file. You can also enable AutoSave to automatically save your file changes, by checking Auto Save in the main File menu.

C/C++ configurations

If you want more control over the C/C++ extension, you can create a

c_cpp_properties.json

file, which will allow you to change settings such as the path to the compiler, include paths, C++ standard (default is C++17), and more.

You can view the C/C++ configuration UI by running the command C/C++: Edit Configurations (UI) from the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)).

This opens the C/C++ Configurations page. When you make changes here, VS Code writes them to a file called

c_cpp_properties.json

in the

.vscode

folder.

You only need to modify the Include path setting if your program includes header files that are not in your workspace or in the standard library path.

Visual Studio Code places these settings in

.vscode/c_cpp_properties.json

. If you open that file directly, it should look something like this:


{ "configurations": [ { "name": "Linux", "includePath": ["${workspaceFolder}/**"], "defines": [], "compilerPath": "/usr/bin/gcc", "cStandard": "c11", "cppStandard": "c++17", "intelliSenseMode": "clang-x64" } ], "version": 4 }

Hướng dẫn cài MinGW và dùng Visual Studio Code để lập trình C/C++ 2022
Hướng dẫn cài MinGW và dùng Visual Studio Code để lập trình C/C++ 2022

Keywords searched by users: hello world c visual studio code

Playing With Vscode: C# Hello World Project – Musings Of A Strange Loop
Playing With Vscode: C# Hello World Project – Musings Of A Strange Loop
C++ Hello World Program| Visual Studio Code | Hello World Program | C++  Tutorial #Codeontrend #C++ - Youtube
C++ Hello World Program| Visual Studio Code | Hello World Program | C++ Tutorial #Codeontrend #C++ – Youtube
C Hello World Program In Visual Studio Code In Hindi - Youtube
C Hello World Program In Visual Studio Code In Hindi – Youtube
C++ Hello World Tutorial In Microsoft Visual Studio. (No Programming  Experience Needed!) - Youtube
C++ Hello World Tutorial In Microsoft Visual Studio. (No Programming Experience Needed!) – Youtube
Creating A Hello World Application With C# And .Net Core In Vs Code -  Authorcode
Creating A Hello World Application With C# And .Net Core In Vs Code – Authorcode
Create A .Net Console Application Using Visual Studio Code - .Net |  Microsoft Learn
Create A .Net Console Application Using Visual Studio Code – .Net | Microsoft Learn
Visual Studio Code C# Beginner Tutorial: Hello World - Youtube
Visual Studio Code C# Beginner Tutorial: Hello World – Youtube
Làm Quen Với Lập Trình C# Và Cách Sử Dụng Visual Studio Code
Làm Quen Với Lập Trình C# Và Cách Sử Dụng Visual Studio Code
Errata Security: Fscking Visual Studio Code Js Hello World
Errata Security: Fscking Visual Studio Code Js Hello World
Building Hello World Html Page Using Vs Code And Live Server Extension -  Youtube
Building Hello World Html Page Using Vs Code And Live Server Extension – Youtube
C Hello World Program - Geeksforgeeks
C Hello World Program – Geeksforgeeks
Github - Saif86/Writing-First-Cpp-Program-In-Microsoft-Visual-Studio---Hello -World-Example: Introduction To Microsoft Visual Studio By Writing Hello  World Program In C++
Github – Saif86/Writing-First-Cpp-Program-In-Microsoft-Visual-Studio—Hello -World-Example: Introduction To Microsoft Visual Studio By Writing Hello World Program In C++
How to print Hello World  using Visual Studio code | C programming
How To Print Hello World Using Visual Studio Code | C Programming – Youtube

See more here: kientrucannam.vn

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *