Chuyển tới nội dung
Home » Visual Studio 2019 C Project | Create The App

Visual Studio 2019 C Project | Create The App

How to Create and Run C Program in Visual Studio

Modern C++ Support

Build apps using C++11, C++14, and C++17

Enjoy support for C++11, C++14 and many C++17 features with market leading performance, build throughput and security. Write code using the power of generic lambda expressions, resumable functions, decltype (auto), extended constexpr and C++ attributes, fold expressions, noexcept in type system, inline variables and other modern features.

Write Linux apps and debug them in real-time

Take advantage of powerful coding and debugging tools to manage code targeting Linux built with GCC, Clang, or another compiler. Debug your Linux applications as they run remotely with GDB. Whether you are building IoT apps or high-performance computing cloud services for Linux, Visual Studio will help you be productive.

Target Android and iOS while staying productive

Leverage the power of Visual Studio 2022 and the debugger to build high-performance Android and/or iOS apps and games in C++, share C++ libraries to target both mobile platforms and Windows, or write once and run across all mobile platforms with Xamarin and C++.

Join the many AAA top game studios already using Visual Studio

Create high-performance games with DirectX to run on Windows devices, or build cross-platform games with a top game engine, such as Unity, Unreal, and Cocos. Join the many wildly-successful game studios that already use Visual Studio to boost your productivity with Visual Studio 2022 and the world-class debugger.

Unparalleled Debugging and Diagnostics

Write the best, bug-free code

Do all the basics like setting breakpoints and stepping through your code, then get more advanced with variable visualization, performance profiling, debugging any local or remote process, and multi-threaded application debugging. Run to click, edit your live code and continue executing without having to rebuild.

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

Add Git source control

Now that you’ve created an app, you might want to add it to a Git repository. Visual Studio makes that process easy with Git tools you can use directly from the IDE.

Tip

Git is the most widely used modern version control system, so whether you’re a professional developer or you’re learning how to code, Git can be very useful. If you’re new to Git, the https://git-scm.com/ website is a good place to start. There, you can find cheat sheets, a popular online book, and Git Basics videos.

To associate your code with Git, start by creating a new Git repository where your code is located:

  1. In the status bar at the bottom-right corner of Visual Studio, select Add to Source Control, and then select Git.

  2. In the Create a Git repository dialog box, sign in to GitHub.

    The repository name auto-populates based on your folder location. Your new repository is private by default, which means you’re the only one who can access it.

    Tip

    Whether your repository is public or private, it’s best to have a remote backup of your code stored securely on GitHub. Even if you aren’t working with a team, a remote repository makes your code available to you from any computer.

  3. Select Create and Push.

    After you create your repository, you see status details in the status bar.

    The first icon with the arrows shows how many outgoing/incoming commits are in your current branch. You can use this icon to pull any incoming commits or push any outgoing commits. You can also choose to view these commits first. To do so, select the icon, and then select View Outgoing/Incoming.

    The second icon with the pencil shows the number of uncommitted changes to your code. You can select this icon to view those changes in the Git Changes window.

To learn more about how to use Git with your app, see the Visual Studio version control documentation.

Review: Code complete

In this tutorial, you made many changes to the Calculator app. The app now handles computing resources more efficiently, and handles most user input errors.

Here’s the complete code, all in one place:


class Calculator { public static double DoOperation(double num1, double num2, string op) { double result = double.NaN; // Default value is "not-a-number" which we use if an operation, such as division, could result in an error. // Use a switch statement to do the math. switch (op) { case "a": result = num1 + num2; break; case "s": result = num1 - num2; break; case "m": result = num1 * num2; break; case "d": // Ask the user to enter a non-zero divisor. if (num2 != 0) { result = num1 / num2; } break; // Return text for an incorrect option entry. default: break; } return result; } } class Program { static void Main(string[] args) { bool endApp = false; // Display title as the C# console calculator app. Console.WriteLine("Console Calculator in C#\r"); Console.WriteLine("------------------------\n"); while (!endApp) { // Declare variables and set to empty. string numInput1 = ""; string numInput2 = ""; double result = 0; // Ask the user to type the first number. Console.Write("Type a number, and then press Enter: "); numInput1 = Console.ReadLine(); double cleanNum1 = 0; while (!double.TryParse(numInput1, out cleanNum1)) { Console.Write("This is not valid input. Please enter an integer value: "); numInput1 = Console.ReadLine(); } // Ask the user to type the second number. Console.Write("Type another number, and then press Enter: "); numInput2 = Console.ReadLine(); double cleanNum2 = 0; while (!double.TryParse(numInput2, out cleanNum2)) { Console.Write("This is not valid input. Please enter an integer value: "); numInput2 = Console.ReadLine(); } // Ask the user to choose an operator. Console.WriteLine("Choose an operator from the following list:"); Console.WriteLine("\ta - Add"); Console.WriteLine("\ts - Subtract"); Console.WriteLine("\tm - Multiply"); Console.WriteLine("\td - Divide"); Console.Write("Your option? "); string op = Console.ReadLine(); try { result = Calculator.DoOperation(cleanNum1, cleanNum2, op); if (double.IsNaN(result)) { Console.WriteLine("This operation will result in a mathematical error.\n"); } else Console.WriteLine("Your result: {0:0.##}\n", result); } catch (Exception e) { Console.WriteLine("Oh no! An exception occurred trying to do the math.\n - Details: " + e.Message); } Console.WriteLine("------------------------\n"); // Wait for the user to respond before closing. Console.Write("Press 'n' and Enter to close the app, or press any other key and Enter to continue: "); if (Console.ReadLine() == "n") endApp = true; Console.WriteLine("\n"); // Friendly linespacing. } return; } }

How to Create C Projects using Visual Studio 2022
How to Create C Projects using Visual Studio 2022

Visual Studio 2017 Installation

In Visual Studio 2017, it’s easy to choose and install just the features you need. And because of its reduced minimum footprint, it installs quickly and with less system impact.

Prerequisites

  • A broadband internet connection. The Visual Studio installer can download several gigabytes of data.

  • A computer that runs Microsoft Windows 7 or later versions. We recommend the latest version of Windows for the best development experience. Make sure that the latest updates are applied to your system before you install Visual Studio.

  • Enough free disk space. Visual Studio requires at least 7 GB of disk space, and can take 50 GB or more if many common options are installed. We recommend you install it on your C: drive.

For details on the disk space and operating system requirements, see Visual Studio Product Family System Requirements. The installer reports how much disk space is required for the options you select.

Download and install

  1. To download the latest Visual Studio 2017 installer for Windows, go to the Microsoft Visual Studio Older downloads page. Expand the 2017 section, and choose the Download button.

    Tip

    The Community edition is for individual developers, classroom learning, academic research, and open source development. For other uses, install Visual Studio 2017 Professional or Visual Studio 2017 Enterprise.

  2. Find the installer file you downloaded and run it. The downloaded file might be displayed in your browser, or you might find it in your Downloads folder. The installer needs Administrator privileges to run. You might see a User Account Control dialog asking you to give permission to let the installer make changes to your system; choose Yes. If you’re having trouble, find the downloaded file in File Explorer, right-click on the installer icon, and choose Run as Administrator from the context menu.

  3. The installer presents you with a list of workloads, which are groups of related options for specific development areas. Support for C++ is now part of optional workloads that aren’t installed by default.

    For C and C++, select the Desktop development with C++ workload and then choose Install.

  4. When the installation completes, choose the Launch button to start Visual Studio.

    The first time you run Visual Studio, you’re asked to sign in with a Microsoft Account. If you don’t have one, you can create one for free. You must also choose a theme. Don’t worry, you can change it later if you want to.

    It might take Visual Studio several minutes to get ready for use the first time you run it. Here’s what it looks like in a quick time-lapse:

    Visual Studio starts faster when you run it again.

  5. When Visual Studio opens, check to see if the flag icon in the title bar is highlighted:

    If it’s highlighted, select it to open the Notifications window. If there are any updates available for Visual Studio, we recommend you install them now. Once the installation is complete, restart Visual Studio.

Select a template type

On the Create a new project dialog, a list of your recently selected templates appears on the left. The templates are sorted by most recently used.

If you’re not selecting from the recently used templates, you can filter all available project templates by Language (for example, C# or C++), Platform (for example, Windows or Azure), and Project type (for example, Desktop or Web). You can also enter search text into the search box to further filter the templates, for example, asp.net.

The tags that appear under each template correspond to the three dropdown filters (language, platform, and project type).

Tip

If you don’t see the template you’re looking for, you might be missing a workload for Visual Studio. To install additional workloads, for example, Azure Development or Mobile Development with .NET, select the Install more tools and features link to open Visual Studio Installer. From there, select the workloads you want to install, and then select Modify. After that, additional project templates will be available to choose from.

Select a template and then select Next.

The tags that appear under each template correspond to the three dropdown filters (language, platform, and project type).

Tip

If you don’t see the template you’re looking for, you might be missing a workload for Visual Studio. To install additional workloads, for example, Azure Development or Mobile Development with .NET, select the Install more tools and features link to open Visual Studio Installer. From there, select the workloads you want to install, and then select Modify. After that, additional project templates will be available to choose from.

Select a template and then select Next.

How to Create C++ Project in Visual Studio 2019 #0
How to Create C++ Project in Visual Studio 2019 #0

Powerful Productivity Features

Browse, edit, and improve your code quickly

Visualize your code with syntax colorization, guidelines, code tooltips, Class View, or Call Hierarchy. Navigate to any code symbol by reference, definition, declaration, and more. Autocomplete your code as you type, quickly repair problems, and refactor your code to your needs. Analyze your code for common issues. Save time that could be better spent.

Tutorial: Create a simple C# console app in Visual Studio (part 1 of 2)

In this tutorial, you use Visual Studio to create and run a C# console app, and explore some features of the Visual Studio integrated development environment (IDE). This tutorial is part 1 of a two-part tutorial series.

In this tutorial, you complete the following tasks:

  • Create a Visual Studio project.
  • Create a C# console app.
  • Debug your app.
  • Close your app.
  • Inspect your complete code.

In part 2, you extend this app to add more projects, learn debugging tricks, and reference third-party packages.

Configure your new project

The Configure your new project dialog has options to name your project (and solution), select a disk location, and select a Framework version (if applicable to the template you chose).

Note

If you create a new project when you already have a project or solution open in Visual Studio, an extra configuration option is available. You can choose to create a new solution or add the new project to the solution that’s already open.

Select Create to create the new project.

How to create C projects in Visual Studio
How to create C projects in Visual Studio

Visual Studio 2015 Installation

To install Visual Studio 2015, go to the Microsoft Visual Studio Older downloads page. Expand the 2015 section, and choose the Download button. Run the downloaded setup program and choose Custom installation and then choose the C++ component. To add C and C++ support to an existing Visual Studio 2015 installation, click on the Windows Start button and type Add Remove Programs. Open the program from the results list and then find your Visual Studio 2015 installation in the list of installed programs. Double-click it, then choose Modify and select the Visual C++ components to install.

In general, we highly recommend that you use the latest version of Visual Studio even if you need to compile your code using the Visual Studio 2015 compiler. For more information, see Use native multi-targeting in Visual Studio to build old projects.

When Visual Studio is running, you’re ready to continue to the next step.

Open the “Create a new project” dialog

There are multiple ways to create a new project in Visual Studio. When you first open Visual Studio, the start window appears, and from there, you can select Create a new project.

If the Visual Studio development environment is already open, you can create a new project by choosing File > New > Project on the menu bar. You can also select the New Project button on the toolbar, or press Ctrl+Shift+N.

Lập trình C trên Visual Studio 2019
Lập trình C trên Visual Studio 2019

Visual Studio 2019 Installation

Welcome to Visual Studio 2019! In this version, it’s easy to choose and install just the features you need. And because of its reduced minimum footprint, it installs quickly and with less system impact.

Note

This topic applies to installation of Visual Studio on Windows. Visual Studio Code is a lightweight, cross-platform development environment that runs on Windows, Mac, and Linux systems. The Microsoft C/C++ for Visual Studio Code extension supports IntelliSense, debugging, code formatting, auto-completion. Visual Studio for Mac doesn’t support Microsoft C++, but does support .NET languages and cross-platform development. For installation instructions, see Install Visual Studio for Mac.

Want to know more about what else is new in this version? See the Visual Studio release notes.

Ready to install? We walk you through it, step-by-step.

Step 1 – Make sure your computer is ready for Visual Studio

Before you begin installing Visual Studio:

  1. Check the system requirements. These requirements help you know whether your computer supports Visual Studio 2019.

  2. Apply the latest Windows updates. These updates ensure that your computer has both the latest security updates and the required system components for Visual Studio.

  3. Reboot. The reboot ensures that any pending installs or updates don’t hinder the Visual Studio install.

  4. Free up space. Remove unneeded files and applications from your %SystemDrive% by, for example, running the Disk Cleanup app.

For questions about running previous versions of Visual Studio side by side with Visual Studio 2019, see the Visual Studio 2019 Platform Targeting and Compatibility page.

Step 2 – Download Visual Studio

Next, download the Visual Studio bootstrapper file. To do so, choose the following button to go to the Visual Studio download page. Choose the Download button, then you can select the edition of Visual Studio that you want.

Step 3 – Install the Visual Studio installer

Run the bootstrapper file you downloaded to install the Visual Studio Installer. This new lightweight installer includes everything you need to both install and customize Visual Studio.

  1. From your Downloads folder, double-click the bootstrapper that matches or is similar to one of the following files:

    • vs_community.exe for Visual Studio Community
    • vs_professional.exe for Visual Studio Professional
    • vs_enterprise.exe for Visual Studio Enterprise

    If you receive a User Account Control notice, choose Yes to allow the bootstrapper to run.

  2. We ask you to acknowledge the Microsoft License Terms and the Microsoft Privacy Statement. Choose Continue.

Step 4 – Choose workloads

After the installer is installed, you can use it to customize your installation by selecting the workloads, or feature sets, that you want. Here’s how.

  1. Find the workload you want in the Installing Visual Studio screen.

    For core C and C++ support, choose the “Desktop development with C++” workload. It comes with the default core editor, which includes basic code editing support for over 20 languages, the ability to open and edit code from any folder without requiring a project, and integrated source code control.

    Other workloads support more kinds of development. For example, choose the “Universal Windows Platform development” workload to create apps that use the Windows Runtime for the Microsoft Store. Choose “Game development with C++” to create games that use DirectX, Unreal, and Cocos2d. Choose “Linux development with C++” to target Linux platforms, including IoT development.

    The Installation details pane lists the included and optional components installed by each workload. You can select or deselect optional components in this list. For example, to support development by using the Visual Studio 2017 or 2015 compiler toolsets, choose the MSVC v141 or MSVC v140 optional components. You can add support for MFC, the experimental Modules language extension, IncrediBuild, and more.

  2. After you choose the workload(s) and optional components you want, choose Install.

    Next, status screens appear that show the progress of your Visual Studio installation.

Tip

At any time after installation, you can install workloads or components that you didn’t install initially. If you have Visual Studio open, go to Tools > Get Tools and Features… which opens the Visual Studio Installer. Or, open Visual Studio Installer from the Start menu. From there, you can choose the workloads or components that you wish to install. Then, choose Modify.

Step 5 – Choose individual components (Optional)

If you don’t want to use the Workloads feature to customize your Visual Studio installation, or you want to add more components than a workload installs, you can do so by installing or adding individual components from the Individual components tab. Choose what you want, and then follow the prompts.

Step 6 – Install language packs (Optional)

By default, the installer program tries to match the language of the operating system when it runs for the first time. To install Visual Studio in a language of your choosing, choose the Language packs tab from the Visual Studio Installer, and then follow the prompts.

Change the installer language from the command line

Another way that you can change the default language is by running the installer from the command line. For example, you can force the installer to run in English by using the following command:

vs_installer.exe --locale en-US

. The installer will remember this setting when it’s run the next time. The installer supports the following language tokens: zh-cn, zh-tw, cs-cz, en-us, es-es, fr-fr, de-de, it-it, ja-jp, ko-kr, pl-pl, pt-br, ru-ru, and tr-tr.

Step 7 – Change the installation location (Optional)

You can reduce the installation footprint of Visual Studio on your system drive. You can choose to move the download cache, shared components, SDKs, and tools to different drives, and keep Visual Studio on the drive that runs it the fastest.

Important

You can select a different drive only when you first install Visual Studio. If you’ve already installed it and want to change drives, you must uninstall Visual Studio and then reinstall it.

Step 8 – Start developing

  1. After Visual Studio installation is complete, choose the Launch button to get started developing with Visual Studio.

  2. On the start window, choose Create a new project.

  3. In the search box, enter the type of app you want to create to see a list of available templates. The list of templates depends on the workload(s) that you chose during installation. To see different templates, choose different workloads.

    You can also filter your search for a specific programming language by using the Language drop-down list. You can filter by using the Platform list and the Project type list, too.

  4. Visual Studio opens your new project, and you’re ready to code!

Create the app

In this section, you complete the following tasks:

  • Explore some basic integer math in C#.
  • Add code to create a basic calculator app.
  • Debug the app to find and fix errors.
  • Refine the code to make it more efficient.

Explore integer math

Start with some basic integer math in C#.

  1. In the code editor, delete the default “Hello World” code.

    Specifically, delete the line that says,


    Console.WriteLine("Hello World!");

    .

  2. In its place, enter the following code:


    int a = 42; int b = 119; int c = a + b; Console.WriteLine(c); Console.ReadKey();

    Notice that when you enter the code, the IntelliSense feature in Visual Studio offers you the option to autocomplete the entry.

  3. Select the green Start button next to Calculator to build and run your program, or press F5.

    A console window opens that reveals the sum of 42 + 119, which is 161.

  4. (Optional) You can change the operator to change the result. For example, you can change the

    operator in the

    int c = a + b;

    line of code tofor subtraction,for multiplication, orfor division. Then, when you run the program, the result changes, too.

  5. Close the console window.

  1. In Solution Explorer, in the right pane, select Program.cs to display the file in the code editor

  2. In the code editor, replace the default “Hello World” code that says


    Console.WriteLine("Hello World!");

    .

    Replace the line with the following code:


    int a = 42; int b = 119; int c = a + b; Console.WriteLine(c); Console.ReadKey();

    If you enter the code, the Visual Studio IntelliSense feature offers you the option to autocomplete the entry.

  3. To build and run your app, press F5, or select the green arrow next to the name Calculator in the top toolbar.

    A console window opens that shows the sum of 42 + 119, which is 161.

  4. Close the console window.

  5. Optionally, you can change the operator to change the result. For example, you can change the

    operator in the

    int c = a + b;

    line of code tofor subtraction,for multiplication, orfor division. When you run the app, the result changes accordingly.

Add code to create a calculator

Continue by adding a more complex set of calculator code to your project.

  1. In the code editor, replace all the code in Program.cs with the following new code:


    using System; namespace Calculator { class Program { static void Main(string[] args) { // Declare variables and then initialize to zero. int num1 = 0; int num2 = 0; // Display title as the C# console calculator app. Console.WriteLine("Console Calculator in C#\r"); Console.WriteLine("------------------------\n"); // Ask the user to type the first number. Console.WriteLine("Type a number, and then press Enter"); num1 = Convert.ToInt32(Console.ReadLine()); // Ask the user to type the second number. Console.WriteLine("Type another number, and then press Enter"); num2 = Convert.ToInt32(Console.ReadLine()); // Ask the user to choose an option. Console.WriteLine("Choose an option from the following list:"); Console.WriteLine("\ta - Add"); Console.WriteLine("\ts - Subtract"); Console.WriteLine("\tm - Multiply"); Console.WriteLine("\td - Divide"); Console.Write("Your option? "); // Use a switch statement to do the math. switch (Console.ReadLine()) { case "a": Console.WriteLine($"Your result: {num1} + {num2} = " + (num1 + num2)); break; case "s": Console.WriteLine($"Your result: {num1} - {num2} = " + (num1 - num2)); break; case "m": Console.WriteLine($"Your result: {num1} * {num2} = " + (num1 * num2)); break; case "d": Console.WriteLine($"Your result: {num1} / {num2} = " + (num1 / num2)); break; } // Wait for the user to respond before closing. Console.Write("Press any key to close the Calculator console app..."); Console.ReadKey(); } } }

  2. Select the Calculator button or press F5 to run your app.

    A console window opens.

  3. In the console window, follow the prompts to add the numbers 42 and 119 together.

    Your app should look similar to the following screenshot:

  1. In the code editor, replace all the code in Program.cs with the following new code:


    // Declare variables and then initialize to zero. int num1 = 0; int num2 = 0; // Display title as the C# console calculator app. Console.WriteLine("Console Calculator in C#\r"); Console.WriteLine("------------------------\n"); // Ask the user to type the first number. Console.WriteLine("Type a number, and then press Enter"); num1 = Convert.ToInt32(Console.ReadLine()); // Ask the user to type the second number. Console.WriteLine("Type another number, and then press Enter"); num2 = Convert.ToInt32(Console.ReadLine()); // Ask the user to choose an option. Console.WriteLine("Choose an option from the following list:"); Console.WriteLine("\ta - Add"); Console.WriteLine("\ts - Subtract"); Console.WriteLine("\tm - Multiply"); Console.WriteLine("\td - Divide"); Console.Write("Your option? "); // Use a switch statement to do the math. switch (Console.ReadLine()) { case "a": Console.WriteLine($"Your result: {num1} + {num2} = " + (num1 + num2)); break; case "s": Console.WriteLine($"Your result: {num1} - {num2} = " + (num1 - num2)); break; case "m": Console.WriteLine($"Your result: {num1} * {num2} = " + (num1 * num2)); break; case "d": Console.WriteLine($"Your result: {num1} / {num2} = " + (num1 / num2)); break; } // Wait for the user to respond before closing. Console.Write("Press any key to close the Calculator console app..."); Console.ReadKey();

  2. Select the Calculator button or press F5 to run your app.

    A console window opens.

  3. In the console window, follow the prompts to add the numbers 42 and 119 together.

    Your app should look similar to the following screenshot:

Add decimal functionality

Now, tweak the code to add more functionality.

The current calculator app only accepts and returns whole numbers. For example, if you run the app and divide the number 42 by the number 119, your result is zero, which isn’t exact.

To fix the code to improve precision by handling decimals:

  1. From Program.cs in the Visual Studio editor, press Ctrl+H to open the Find and Replace control.

  2. Type int in the control, and type float in the Replace field.

  3. Select the icons for Match case and Match whole word in the control, or press Alt+C and Alt+W.

  4. Select the Replace all icon or press Alt+A to run the search and replace.

  5. Run your calculator app again, and divide the number 42 by the number 119.

    The app now returns a decimal number instead of zero.

    Now the app can produce decimal results. Make a few more tweaks to the code so the app can calculate decimals too.

  6. Use the Find and Replace control to change each instance of the


    float

    variable to

    double

    , and to change each instance of the

    Convert.ToInt32

    method to

    Convert.ToDouble

    .

  7. Run your calculator app, and divide the number 42.5 by the number 119.75.

    The app now accepts decimal values, and returns a longer decimal numeral as its result.

    In the Revise the code section, you reduce the number of decimal places in the results.

How to run first C# Console Application Project on Visual Studio 2019
How to run first C# Console Application Project on Visual Studio 2019

Open the “Create a new project” dialog

There are multiple ways to create a new project in Visual Studio. When you first open Visual Studio, the start window appears, and from there, you can select Create a new project.

If the Visual Studio development environment is already open, you can create a new project by choosing File > New > Project on the menu bar. You can also select the New Project button on the toolbar, or press Ctrl+Shift+N.

Visual Studio 2022 Installation

Welcome to Visual Studio 2022! In this version, it’s easy to choose and install just the features you need. And because of its reduced minimum footprint, it installs quickly and with less system impact.

Note

This topic applies to installation of Visual Studio on Windows. Visual Studio Code is a lightweight, cross-platform development environment that runs on Windows, Mac, and Linux systems. The Microsoft C/C++ for Visual Studio Code extension supports IntelliSense, debugging, code formatting, auto-completion. Visual Studio for Mac doesn’t support Microsoft C++, but does support .NET languages and cross-platform development. For installation instructions, see Install Visual Studio for Mac.

Want to know more about what else is new in this version? See the Visual Studio release notes.

Ready to install? We walk you through it, step-by-step.

Step 1 – Make sure your computer is ready for Visual Studio

Before you begin installing Visual Studio:

  1. Check the system requirements. These requirements help you know whether your computer supports Visual Studio 2022.

  2. Apply the latest Windows updates. These updates ensure that your computer has both the latest security updates and the required system components for Visual Studio.

  3. Reboot. The reboot ensures that any pending installs or updates don’t hinder the Visual Studio install.

  4. Free up space. Remove unneeded files and applications from your %SystemDrive% by, for example, running the Disk Cleanup app.

For questions about running previous versions of Visual Studio side by side with Visual Studio 2022, see the Visual Studio 2022 Platform Targeting and Compatibility page.

Step 2 – Download Visual Studio

Next, download the Visual Studio bootstrapper file. To do so, choose the following button to go to the Visual Studio download page. Select the edition of Visual Studio that you want and choose the Free trial or Free download button.

Step 3 – Install the Visual Studio installer

Run the bootstrapper file you downloaded to install the Visual Studio Installer. This new lightweight installer includes everything you need to both install and customize Visual Studio.

  1. From your Downloads folder, double-click the bootstrapper that matches or is similar to one of the following files:

    • vs_community.exe for Visual Studio Community
    • vs_professional.exe for Visual Studio Professional
    • vs_enterprise.exe for Visual Studio Enterprise

    If you receive a User Account Control notice, choose Yes to allow the bootstrapper to run.

  2. We ask you to acknowledge the Microsoft License Terms and the Microsoft Privacy Statement. Choose Continue.

Step 4 – Choose workloads

After the installer is installed, you can use it to customize your installation by selecting the workloads, or feature sets, that you want. Here’s how.

  1. Find the workload you want in the Installing Visual Studio screen.

    For core C and C++ support, choose the “Desktop development with C++” workload. It comes with the default core editor, which includes basic code editing support for over 20 languages, the ability to open and edit code from any folder without requiring a project, and integrated source code control.

    Other workloads support more kinds of development. For example, choose the “Universal Windows Platform development” workload to create apps that use the Windows Runtime for the Microsoft Store. Choose “Game development with C++” to create games that use DirectX, Unreal, and Cocos2d. Choose “Linux development with C++” to target Linux platforms, including IoT development.

    The Installation details pane lists the included and optional components installed by each workload. You can select or deselect optional components in this list. For example, to support development by using the Visual Studio 2017 or 2015 compiler toolsets, choose the MSVC v141 or MSVC v140 optional components. You can add support for MFC, the experimental Modules language extension, IncrediBuild, and more.

  2. After you choose the workload(s) and optional components you want, choose Install.

    Next, status screens appear that show the progress of your Visual Studio installation.

Tip

At any time after installation, you can install workloads or components that you didn’t install initially. If you have Visual Studio open, go to Tools > Get Tools and Features… which opens the Visual Studio Installer. Or, open Visual Studio Installer from the Start menu. From there, you can choose the workloads or components that you wish to install. Then, choose Modify.

Step 5 – Choose individual components (Optional)

If you don’t want to use the Workloads feature to customize your Visual Studio installation, or you want to add more components than a workload installs, you can do so by installing or adding individual components from the Individual components tab. Choose what you want, and then follow the prompts.

Step 6 – Install language packs (Optional)

By default, the installer program tries to match the language of the operating system when it runs for the first time. To install Visual Studio in a language of your choosing, choose the Language packs tab from the Visual Studio Installer, and then follow the prompts.

Change the installer language from the command line

Another way that you can change the default language is by running the installer from the command line. For example, you can force the installer to run in English by using the following command:

vs_installer.exe --locale en-US

. The installer will remember this setting when it’s run the next time. The installer supports the following language tokens: zh-cn, zh-tw, cs-cz, en-us, es-es, fr-fr, de-de, it-it, ja-jp, ko-kr, pl-pl, pt-br, ru-ru, and tr-tr.

Step 7 – Change the installation location (Optional)

You can reduce the installation footprint of Visual Studio on your system drive. You can choose to move the download cache, shared components, SDKs, and tools to different drives, and keep Visual Studio on the drive that runs it the fastest.

Important

You can select a different drive only when you first install Visual Studio. If you’ve already installed it and want to change drives, you must uninstall Visual Studio and then reinstall it.

Step 8 – Start developing

  1. After Visual Studio installation is complete, choose the Launch button to get started developing with Visual Studio.

  2. On the start window, choose Create a new project.

  3. In the search box, enter the type of app you want to create to see a list of available templates. The list of templates depends on the workload(s) that you chose during installation. To see different templates, choose different workloads.

    You can also filter your search for a specific programming language by using the Language drop-down list. You can filter by using the Platform list and the Project type list, too.

  4. Visual Studio opens your new project, and you’re ready to code!

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

Create a project

To start, create a C# application project. The project type comes with all the template files you need.

  1. Open Visual Studio, and select Create a new project in the Start window.

  2. In the Create a new project window, choose C# from the Language list. Next, choose Windows from the Platform list and Console from the project types list.

    After you apply the language, platform, and project type filters, choose the Console Application template, and then select Next.

    Note

    If you don’t see the Console Application template, select Install more tools and features.

    In the Visual Studio Installer, select the .NET Core cross-platform development workload.

    Select Modify in the Visual Studio Installer. You might be prompted to save your work. Select Continue to install the workload.

    Return to step 2 in this “Create a project” procedure.

  3. In the Configure your new project window, type or enter Calculator in the Project name box. Then, select Next.

  4. In the Additional information window, verify that .NET Core 3.1 appears in the Target Framework field. Then, select Create.

Visual Studio opens your new project, which includes default “Hello World” code. To view it in the editor, select the code file Program.cs in the Solution Explorer window, which is typically on the right-hand side of Visual Studio.

The default “Hello World” code calls the WriteLine method to display the literal string “Hello, World!” in the console window. If you press F5, you can run the default program in Debug mode. After the application runs in the debugger, the console window stays open. Press any key to close the console window.

  1. Open Visual Studio, and select Create a new project in the Start window.

  2. In the Create a new project window, select All languages, and then choose C# from the dropdown list. Choose Windows from the All platforms list, and choose Console from the All project types list.

    After you apply the language, platform, and project type filters, choose the Console App template, and then select Next.

    Note

    If you don’t see the Console App template, select Install more tools and features.

    In the Visual Studio Installer, select the .NET desktop development workload.

    Select Modify in the Visual Studio Installer. You might be prompted to save your work. Select Continue to install the workload.

    Return to step 2 in this “Create a project” procedure.

  3. In the Configure your new project window, type or enter Calculator in the Project name box, and then select Next.

  4. In the Additional information window, select .NET 8.0 for the Target Framework field. Then, select Create.

Visual Studio opens your new project, which includes default “Hello World” code. To view it in the editor, select the code file Program.cs in the Solution Explorer window, which is typically on the right-hand side of Visual Studio.

The single code statement calls the WriteLine method to display the literal string “Hello, World!” in the console window. If you press F5, you can run the default program in Debug mode. After the application runs in the debugger, the console window stays open. Press any key to close the console window.

Note

Starting with .NET 6, new projects using the console template generate different code than previous versions. To learn more, see the New C# templates generate top-level statements page.

Feedback

Submit and view feedback for

This document is for compiling projects using C. If you wish to use C++, see Building a Microsoft Visual Studio C++ Project for Your c-tree Application.

To compile C programs with Microsoft Visual Studio, you must have the C++ component installed. This seems counter-intuitive, but the C++ component includes both the C compiler and the C++ compiler.

The New Project Wizard in modern versions of Microsoft Visual Studio does not contain templates that explicitly create C projects — all of the C-type templates specify C++ as the programming language (see the following picture). A workaround is required to create a new C project. The workaround involves adding a new C++ file to your project, but changing the filename’s extension from .cpp to .c before you press the button that actually creates the new file.

This workaround works because the default behavior of Visual Studio is to compile source code files that have a “.c” file name extension as C, and to compile files that have a .cpp extension as C++. As long as your source code file names all end in .c, Visual Studio will use the C compiler (and not the C++ compiler) to compile them. Note that this default behavior can be changed for a project by going to the project’s Properties window and navigating to Configuration Properties > C/C++ > Advanced, and then changing the Compile As entry from Default to Compile as C Code (/TC).

With this background, the steps to create a new c-treeDB project in Visual Studio are as follows:

Explicit details of these steps follow.

Develop C and C++ applications

Build modern C and C++ apps for Windows using tools of your choice, including MSVC, Clang, CMake, and MSBuild

Install the components you need for building C and C++ apps

  • Contains C/C++ components for desktop, mobile, Linux, and game development
  • Get a light and simplified installation
  • Add more components during installation, or

By downloading, you agree to the licensing terms for the Visual Studio edition you select below. We also offer the ability to download software with Visual Studio. This software is licensed separately, as set out in the 3rd Party Notices or in its accompanying license. By downloading, you also agree to those licenses.

Senior Programmers vs Junior Developers #shorts
Senior Programmers vs Junior Developers #shorts

Debug the app

You improved your basic calculator app, but your app doesn’t yet handle exceptions, such as user input errors. For example, if users try to divide by zero, or enter an unexpected character, the app might stop working, return an error, or return an unexpected non-numeric result.

Let’s walk through a few common user input errors, locate them in the debugger if they appear there, and fix them in the code.

Tip

For more information about the debugger and how it works, see First look at the Visual Studio debugger.

Fix the “divide by zero” error

If you try to divide a number by zero, the console app might freeze, and then shows you what’s wrong in the code editor.

Note

Sometimes the app doesn’t freeze, and the debugger doesn’t show a divide-by-zero error. Instead, the app might return an unexpected nonnumeric result, such as an infinity symbol. The following code fix still applies.

Let’s change the code to handle this error. In Program.cs, replace the code for

case "d":

with the following code:


// Ask the user to enter a non-zero divisor until they do so. while (num2 == 0) { Console.WriteLine("Enter a non-zero divisor: "); num2 = Convert.ToInt32(Console.ReadLine()); } Console.WriteLine($"Your result: {num1} / {num2} = " + (num1 / num2)); break; }

After you replace the code, the section with the

switch

statement should look similar to the following screenshot:

Now, when you divide any number by zero, the app asks for another number, and keeps asking until you provide a nonzero number.

Fix the “format” error

If you enter an alphabetic character when the app expects a numeric character, the app freezes. Visual Studio shows you what’s wrong in the code editor.

To prevent this exception, you can refactor the code you’ve previously entered.

Revise the code

Rather than rely on the

program

class to handle all the code, you can divide your app into two classes:

Calculator

and

Program

.

The

Calculator

class handles the bulk of the calculation work, and the

Program

class handles the user interface and error-handling work.

Let’s get started.

  1. In Program.cs, delete everything and add the following new


    Calculator

    class:

    class Calculator { public static double DoOperation(double num1, double num2, string op) { double result = double.NaN; // Default value is "not-a-number" if an operation, such as division, could result in an error. // Use a switch statement to do the math. switch (op) { case "a": result = num1 + num2; break; case "s": result = num1 - num2; break; case "m": result = num1 * num2; break; case "d": // Ask the user to enter a non-zero divisor. if (num2 != 0) { result = num1 / num2; } break; // Return text for an incorrect option entry. default: break; } return result; } }

  2. Also add a new


    Program

    class, as follows:

    class Program { static void Main(string[] args) { bool endApp = false; // Display title as the C# console calculator app. Console.WriteLine("Console Calculator in C#\r"); Console.WriteLine("------------------------\n"); while (!endApp) { // Declare variables and set to empty. string numInput1 = ""; string numInput2 = ""; double result = 0; // Ask the user to type the first number. Console.Write("Type a number, and then press Enter: "); numInput1 = Console.ReadLine(); double cleanNum1 = 0; while (!double.TryParse(numInput1, out cleanNum1)) { Console.Write("This is not valid input. Please enter an integer value: "); numInput1 = Console.ReadLine(); } // Ask the user to type the second number. Console.Write("Type another number, and then press Enter: "); numInput2 = Console.ReadLine(); double cleanNum2 = 0; while (!double.TryParse(numInput2, out cleanNum2)) { Console.Write("This is not valid input. Please enter an integer value: "); numInput2 = Console.ReadLine(); } // Ask the user to choose an operator. Console.WriteLine("Choose an operator from the following list:"); Console.WriteLine("\ta - Add"); Console.WriteLine("\ts - Subtract"); Console.WriteLine("\tm - Multiply"); Console.WriteLine("\td - Divide"); Console.Write("Your option? "); string op = Console.ReadLine(); try { result = Calculator.DoOperation(cleanNum1, cleanNum2, op); if (double.IsNaN(result)) { Console.WriteLine("This operation will result in a mathematical error.\n"); } else Console.WriteLine("Your result: {0:0.##}\n", result); } catch (Exception e) { Console.WriteLine("Oh no! An exception occurred trying to do the math.\n - Details: " + e.Message); } Console.WriteLine("------------------------\n"); // Wait for the user to respond before closing. Console.Write("Press 'n' and Enter to close the app, or press any other key and Enter to continue: "); if (Console.ReadLine() == "n") endApp = true; Console.WriteLine("\n"); // Friendly linespacing. } return; } }

  3. Select the Calculator button or press F5 to run your app.

  4. Follow the prompts and divide the number 42 by the number 119. Your results should look similar to the following screenshot:

    You can now run more calculations until you choose to close the console app. There are also fewer decimal places in the results. And if you enter an incorrect character, you get an appropriate error response.

Keywords searched by users: visual studio 2019 c project

How To Create C Projects In Visual Studio - Youtube
How To Create C Projects In Visual Studio – Youtube
Tutorial: Create A Simple C# Console App - Visual Studio (Windows) |  Microsoft Learn
Tutorial: Create A Simple C# Console App – Visual Studio (Windows) | Microsoft Learn
Create New Project In Visual Studio 2019 - Youtube
Create New Project In Visual Studio 2019 – Youtube
Create A Linux Msbuild C++ Project In Visual Studio | Microsoft Learn
Create A Linux Msbuild C++ Project In Visual Studio | Microsoft Learn
Setup An Assembly Project On Visual Studio 2019 & 2022 – Programming Haven
Setup An Assembly Project On Visual Studio 2019 & 2022 – Programming Haven
How To Create A C# Project With Visual Studio Code? - Tutorialkart
How To Create A C# Project With Visual Studio Code? – Tutorialkart
Windows C++ Project Property Page Reference - Visual Studio | Microsoft  Learn
Windows C++ Project Property Page Reference – Visual Studio | Microsoft Learn
New Start Window And New Project Dialog Experience In Visual Studio 2019 -  C++ Team Blog
New Start Window And New Project Dialog Experience In Visual Studio 2019 – C++ Team Blog
Visual Studio 2019 Version 16.3 Preview 2 And Visual Studio For Mac Version  8.3 Preview 2 Released! - Visual Studio Blog
Visual Studio 2019 Version 16.3 Preview 2 And Visual Studio For Mac Version 8.3 Preview 2 Released! – Visual Studio Blog
How To Create C Programs Using Latest Visual Studio 2019 - Youtube
How To Create C Programs Using Latest Visual Studio 2019 – Youtube
Visual Studio - Wikipedia
Visual Studio – Wikipedia
How To Migrate C++ Dll From Visual Studio 2017 & Rhino 6 To Visual Studio  2019 & Rhino 7 - Rhino Developer - Mcneel Forum
How To Migrate C++ Dll From Visual Studio 2017 & Rhino 6 To Visual Studio 2019 & Rhino 7 – Rhino Developer – Mcneel Forum
Uipath Activity Creator - Visual Studio 2019 - A Problem Was Encountered  Creating Sub Project - Help - Uipath Community Forum
Uipath Activity Creator – Visual Studio 2019 – A Problem Was Encountered Creating Sub Project – Help – Uipath Community Forum
Visual Studio 2019 Goes Live With C++, Python Shared Editing | Ars Technica
Visual Studio 2019 Goes Live With C++, Python Shared Editing | Ars Technica
Walkthrough: Create A Traditional Windows Desktop Application (C++) |  Microsoft Learn
Walkthrough: Create A Traditional Windows Desktop Application (C++) | Microsoft Learn
Cách Tạo Dự Án Console C# Trong Visual Studio | Dammio
Cách Tạo Dự Án Console C# Trong Visual Studio | Dammio
How To Migrate C++ Dll From Visual Studio 2017 & Rhino 6 To Visual Studio  2019 & Rhino 7 - Rhino Developer - Mcneel Forum
How To Migrate C++ Dll From Visual Studio 2017 & Rhino 6 To Visual Studio 2019 & Rhino 7 – Rhino Developer – Mcneel Forum

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 *