Skip to content

Commit 3317898

Browse files
Merge pull request #5835 from MicrosoftDocs/main638969654879875353sync_temp
For protected branch, push strategy should use PR and merge to target branch method to work around git push error
2 parents 1d63b16 + 02127b9 commit 3317898

23 files changed

+353
-38
lines changed

docs/build/image.png

27 KB
Loading
7.14 KB
Loading
16.3 KB
Loading
21.8 KB
Loading
11 KB
Loading

docs/build/media/life-exe.png

5.07 KB
Loading
Lines changed: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
1+
---
2+
title: Use AI to create a C++ console application in Visual Studio
3+
description: "Learn how to use GitHub Copilot to create a C++ app using Microsoft C++ in Visual Studio."
4+
ms.date: 10/24/2025
5+
ms.topic: "tutorial"
6+
ms.custom:
7+
- ai-assisted
8+
- copilot-scenario-highlight
9+
---
10+
11+
# Use AI to create a C++ console application in Visual Studio
12+
13+
This tutorial shows you how to use GitHub Copilot to quickly create a C++ console application in Visual Studio. You create a console version of Conway's Game of Life, a classic cellular automaton.
14+
15+
Conway's Game of Life was devised by mathematician John Conway. It consists of a grid of cells that can be either alive or dead. The game evolves automatically based on simple rules and produces complex, evolving patterns that demonstrate how intricate behavior can emerge from basic mathematical rules.
16+
17+
GitHub Copilot is an AI-powered coding assistant that helps you write code faster, reduce errors, and explore new solutions. Some benefits of using Copilot when coding in C++:
18+
- Generate entire C++ functions or classes as you type.
19+
- Suggest code completions based on plain-language comments or prompts.
20+
- Get help with complex algorithms, data structures, and standard library usage.
21+
- Learn new APIs and modern C++ patterns through in-context examples.
22+
- Receive context-aware suggestions based on your comments or code.
23+
- Debug errors in your code.
24+
- Simplify and refactor existing code.
25+
26+
## Prerequisites
27+
28+
- Visual Studio 2022 or later with the **Desktop development with C++** workload installed.
29+
- GitHub Copilot. For more information, see [Get started with GitHub Copilot](/visualstudio/ide/visual-studio-github-copilot-get-started).
30+
31+
To verify you have the C++ workload installed:
32+
1. Open Visual Studio Installer
33+
1. Select **Modify** next to your Visual Studio installation
34+
1. Ensure **Desktop development with C++** is checked:
35+
36+
:::image type="content" source="media/desktop-development-cpp-workload.png" alt-text="Screenshot of the Visual Studio Installer with the Workloads tab selected. Desktop development with c++ is selected.":::
37+
38+
1. If it isn't installed, select it and choose **Modify**.
39+
40+
For more information about installing Copilot, see [Manage GitHub Copilot installation and state](/visualstudio/ide/visual-studio-github-copilot-install-and-states).
41+
42+
## Create a project
43+
44+
Visual Studio uses *projects* to organize the code for an app, and *solutions* to organize your projects. A project contains all the options, configurations, and rules used to build your apps. It manages the relationship between all the project's files and any external files. To create your app, first, create a new project and solution.
45+
46+
1. Open Visual Studio and select **Create a new project**.
47+
1. Search for "Console App" and select the **Console App** template for C++.
48+
49+
:::image type="complex" source="media/vs2019-choose-console-app.png" alt-text="Screenshot of the Create a new project dialog.":::
50+
The Create a new project dialog is shown with the Console App template selected. The template says: Run code in a windows terminal. Prints hello world by default. Has the tags c++, Windows, and Console.
51+
:::image-end:::
52+
53+
1. Select **Next**.
54+
1. Set the project name to **Life** and choose the location for the project.
55+
1. Select **Create**.
56+
1. Once the project opens, find the `Life.cpp` file in Solution Explorer.
57+
1. Open `Life.cpp` and delete the default "Hello, World!" code to start with a clean slate.
58+
59+
## Use Copilot to create an app
60+
61+
You prompt Copilot by describing the functionality you want. In this section, you'll learn how to prompt Copilot to generate an implementation of Conway's Game of Life.
62+
63+
1. Open the Copilot chat window by selecting the Copilot icon in the toolbar:
64+
65+
:::image type="content" source="media/github-copilot-open-chat.png" alt-text="Screenshot of the GitHub icon dropdown. Open Chat Window is selected.":::
66+
67+
1. In the chat window, enter the following prompt:
68+
```copilot-prompt
69+
Create a C++ console application that implements Conway's Game of Life. The program should:
70+
- Use a 40x20 grid displayed with asterisks (*) for live cells and spaces for dead cells
71+
- Start with a random initial pattern
72+
- Display each generation for 500ms before showing the next
73+
- Allow the user to press any key to exit the program
74+
- Include proper headers and use standard C++ practices
75+
- Clear the console between generations to provide an animation effect
76+
```
77+
1. Copilot generates C++ code for Conway's Game of Life.
78+
1. Copy the generated code and paste it into your empty `Life.cpp` file.
79+
1. Build the project by pressing **F6** or selecting **Build > Build Solution**.
80+
1. Run the program by pressing **F5** or **Ctrl+F5**.
81+
82+
> [!NOTE]
83+
> The exact code generated by Copilot may vary slightly from run to run and model to model, but the core functionality should be consistent. If the generated code doesn't compile immediately, you can ask Copilot to fix any compilation errors. Copilot is powered by AI, so surprises and mistakes are possible. For more information, see [Copilot FAQs](https://aka.ms/copilot-general-use-faqs).
84+
85+
### Example of typical generated code structure
86+
87+
Your generated code will likely include these key components:
88+
89+
- Headers for console manipulation, random number generation, and timing
90+
- A 2D array or vector to represent the game grid
91+
- Functions to initialize the grid, apply Conway's rules, and display the current state
92+
- A main loop that continues until a key is pressed
93+
94+
Here's an example of the code that Copilot generated using the previous prompt:
95+
96+
```cpp
97+
// Code generated by GitHub Copilot
98+
#include <iostream>
99+
#include <chrono>
100+
#include <thread>
101+
#include <conio.h> // for _kbhit and _getch
102+
103+
using namespace std;
104+
105+
int step = 0;
106+
const int rows = 20;
107+
const int cols = 40;
108+
109+
void printGrid(int grid[rows][cols])
110+
{
111+
cout << "Step: " << step << endl;
112+
for (int i = 0; i < rows; ++i)
113+
{
114+
for (int j = 0; j < cols; ++j)
115+
{
116+
cout << (grid[i][j] ? '*' : ' ');
117+
}
118+
cout << endl;
119+
}
120+
}
121+
122+
int countNeighbors(int grid[rows][cols], int x, int y)
123+
{
124+
int count = 0;
125+
for (int i = -1; i <= 1; ++i)
126+
{
127+
for (int j = -1; j <= 1; ++j)
128+
{
129+
if (i == 0 && j == 0)
130+
{
131+
continue;
132+
}
133+
134+
int ni = x + i;
135+
int nj = y + j;
136+
if (ni >= 0 && ni < rows && nj >= 0 && nj < cols)
137+
{
138+
count += grid[ni][nj];
139+
}
140+
}
141+
}
142+
return count;
143+
}
144+
145+
void updateGrid(int grid[rows][cols])
146+
{
147+
int newGrid[rows][cols] = { 0 };
148+
for (int i = 0; i < rows; ++i)
149+
{
150+
for (int j = 0; j < cols; ++j)
151+
{
152+
int neighbors = countNeighbors(grid, i, j);
153+
if (grid[i][j] == 1)
154+
{
155+
newGrid[i][j] = (neighbors < 2 || neighbors > 3) ? 0 : 1;
156+
}
157+
else
158+
{
159+
newGrid[i][j] = (neighbors == 3) ? 1 : 0;
160+
}
161+
}
162+
}
163+
// Copy newGrid back to grid
164+
for (int i = 0; i < rows; ++i)
165+
{
166+
for (int j = 0; j < cols; ++j)
167+
{
168+
grid[i][j] = newGrid[i][j];
169+
}
170+
}
171+
}
172+
173+
int main()
174+
{
175+
int grid[rows][cols] = { 0 };
176+
177+
// Initial configuration (a simple glider)
178+
grid[1][2] = 1;
179+
grid[2][3] = 1;
180+
grid[3][1] = 1;
181+
grid[3][2] = 1;
182+
grid[3][3] = 1;
183+
184+
while (true)
185+
{
186+
if (_kbhit()) // Check if a key has been pressed
187+
{
188+
_getch(); // Consume the key
189+
break; // Exit the loop
190+
}
191+
printGrid(grid);
192+
updateGrid(grid);
193+
std::this_thread::sleep_for(std::chrono::milliseconds(100));
194+
cout << "\033[H\033[J"; // Clear the screen
195+
step++;
196+
}
197+
198+
return 0;
199+
}
200+
```
201+
When you run the application, you should see an animated display of Conway's Game of Life with patterns evolving over time. To exit the program, press a key.
202+
203+
:::image type="content" source="./media/life-exe.png" alt-text="Screenshot of Conway Life running in a command window, displaying the evolving grid of cells.":::
204+
205+
In the preceding code example, the code generates a warning: `Return value ignored: '_getch'`. You can ask Copilot to fix it. Select the code editor and press **Alt+/** (Windows) to open the Copilot chat, then enter:
206+
207+
:::image type="content" source="./media/github-copilot-fix-warning.png" alt-text="Screenshot of the Copilot chat window. The text: Fix warning C6031 is in the chat window.":::
208+
209+
Copilot suggests a fix to handle the return value properly. To accept the changes, select **Tab**:
210+
211+
:::image type="content" source="./media/github-copilot-fix-warning-accept.png" alt-text="Screenshot of the Copilot proposed changes. Tab to accept. Alt+Del to discard.":::
212+
213+
Congratulations! You successfully used GitHub Copilot to create a fully functional Conway's Game of Life console application in C++. You learned how to:
214+
215+
- Craft an effective prompt to generate C++ code
216+
- Use Copilot's chat interface to create an entire application from scratch
217+
- Fix compilation warnings with AI assistance
218+
219+
## Improve your prompting skills
220+
221+
For better results with Copilot, see these prompting resources:
222+
223+
- [GitHub Copilot documentation](https://docs.github.com/en/copilot) - Official best practices and tips
224+
- [OpenAI's GPT best practices](https://platform.openai.com/docs/guides/prompt-engineering) - General AI prompting techniques
225+
- [Copilot prompting guide](https://github.blog/2023-06-20-how-to-write-better-prompts-for-github-copilot/) - Specific guidance for code generation
226+
227+
## Troubleshooting
228+
229+
### Missing console app template
230+
231+
The **New Project** dialog should show a **Console App** template that has **C++**, **Windows**, and **Console** tags. If you don't see it, it might be filtered out of the list, or it might not be installed. First, check the filter dropdowns at the top of the list of templates. Set them to **C++**, **Windows**, and **Console**. The C++ **Console App** template should appear; otherwise, the **Desktop development with C++** workload isn't installed.
232+
233+
To install **Desktop development with C++**, you can run the installer right from the **Create a new project** dialog. Choose the **Install more tools and features** link at the bottom of the **Create a new project** dialog, beneath the list of templates. If the **User Account Control** dialog requests permissions, choose **Yes**. In the installer, make sure the **Desktop development with C++** workload is checked. Then choose **Modify** to update your Visual Studio installation.
234+
235+
### Copilot not responding
236+
237+
- Ensure you have an active GitHub Copilot subscription.
238+
- Try signing out and back into your GitHub account in Visual Studio
239+
240+
### Generated code doesn't compile
241+
242+
- Ask Copilot to fix specific compilation errors by pasting the error message into Copilot chat.
243+
- Try refining your prompt to be more specific about what you want the app to do.
244+
245+
## Next steps
246+
247+
- [GitHub Copilot in Visual Studio](/visualstudio/ide/visual-studio-github-copilot-install-and-states)
248+
- [GitHub Copilot documentation](https://docs.github.com/en/copilot) - Dive deeper into AI-assisted development
249+
- [Awesome ChatGPT Prompts](https://github.com/f/awesome-chatgpt-prompts) - Community-driven prompting examples for inspiration

docs/build/vscpp-step-0-installation.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ Some benefits of using Copilot for your C++ coding scenarios:
128128
- Debug errors in your code.
129129
- Simplify and refactor existing code.
130130

131-
To install GitHub Copilot, see [Manage GitHub Copilot installation and state](/visualstudio/ide/visual-studio-github-copilot-install-and-states).
131+
To try GitHub copilot to create a C++ app, follow the instructions in [Use AI to create a C++ console application in Visual Studio](../build/use-github-copilot-create-cpp-console-app.md).
132132

133133
::: moniker-end
134134

@@ -225,9 +225,7 @@ You can reduce the installation footprint of Visual Studio on your system drive.
225225
### Step 8 - Start developing
226226

227227
1. After Visual Studio installation is complete, choose the **Launch** button to get started developing with Visual Studio.
228-
229228
1. On the start window, choose **Create a new project**.
230-
231229
1. 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 workloads that you chose during installation. To see different templates, choose different workloads.
232230

233231
You can also filter your search for a specific programming language by using the **Language** dropdown list. You can filter by using the **Platform** list and the **Project type** list, too.

docs/c-runtime-library/reference/ctime-ctime32-ctime64-wctime-wctime32-wctime64.md

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ api_type: ["DLLExport"]
88
topic_type: ["apiref"]
99
f1_keywords: ["_wctime64", "_ctime32", "_tctime", "_wctime", "_wctime32", "_tctime64", "_ctime64", "ctime"]
1010
helpviewer_keywords: ["tctime64 function", "_ctime32 function", "ctime32 function", "_wctime function", "wctime64 function", "_tctime64 function", "_tctime32 function", "_ctime64 function", "_wctime64 function", "ctime function", "wctime32 function", "ctime64 function", "_wctime32 function", "_tctime function", "tctime32 function", "tctime function", "wctime function", "time, converting"]
11-
ms.assetid: 2423de37-a35c-4f0a-a378-3116bc120a9d
1211
---
1312
# `ctime`, `_ctime32`, `_ctime64`, `_wctime`, `_wctime32`, `_wctime64`
1413

@@ -17,10 +16,10 @@ Convert a time value to a string and adjust for local time zone settings. More s
1716
## Syntax
1817

1918
```C
20-
char *ctime( const time_t *sourceTime );
19+
char *ctime( const time_t *sourceTime ); // See note in remarks section about linkage
2120
char *_ctime32( const __time32_t *sourceTime );
2221
char *_ctime64( const __time64_t *sourceTime );
23-
wchar_t *_wctime( const time_t *sourceTime );
22+
wchar_t *_wctime( const time_t *sourceTime ); // See note in remarks section about linkage
2423
wchar_t *_wctime32( const __time32_t *sourceTime );
2524
wchar_t *_wctime64( const __time64_t *sourceTime );
2625
```
@@ -62,9 +61,14 @@ These functions validate their parameters. If *`sourceTime`* is a null pointer,
6261

6362
By default, this function's global state is scoped to the application. To change this behavior, see [Global state in the CRT](../global-state.md).
6463

64+
> [!Note]
65+
> When you use Windows SDK version 10.0.26100.6901 and Visual Studio 2026 or later together, `ctime` and `_wctime` are no longer `static inline` (internal linkage). Instead, they're `inline` (external linkage).\
66+
> To return to the previous behavior, `#define _STATIC_INLINE_UCRT_FUNCTIONS=1` before including any CRT headers. By default, `_STATIC_INLINE_UCRT_FUNCTIONS` is set to 0.\
67+
> This change increases UCRT conformance with the C++ standard and improves compatibility with C++ modules.
68+
6569
### Generic-text routine mappings
6670

67-
| TCHAR.H routine | `_UNICODE` and `_MBCS` not defined | `_MBCS` defined | `_UNICODE` defined |
71+
| `TCHAR.H` routine | `_UNICODE` and `_MBCS` not defined | `_MBCS` defined | `_UNICODE` defined |
6872
|---|---|---|---|
6973
| `_tctime` | **`ctime`** | **`ctime`** | **`_wctime`** |
7074
| `_tctime32` | **`_ctime32`** | **`_ctime32`** | **`_wctime32`** |
@@ -74,12 +78,12 @@ By default, this function's global state is scoped to the application. To change
7478

7579
| Routine | Required header |
7680
|---|---|
77-
| **`ctime`** | \<time.h> |
78-
| **`_ctime32`** | \<time.h> |
79-
| **`_ctime64`** | \<time.h> |
80-
| **`_wctime`** | \<time.h> or \<wchar.h> |
81-
| **`_wctime32`** | \<time.h> or \<wchar.h> |
82-
| **`_wctime64`** | \<time.h> or \<wchar.h> |
81+
| **`ctime`** | `<time.h>` |
82+
| **`_ctime32`** | `<time.h>` |
83+
| **`_ctime64`** | `<time.h>` |
84+
| **`_wctime`** | `<time.h>` or `<wchar.h>` |
85+
| **`_wctime32`** | `<time.h>` or `<wchar.h>` |
86+
| **`_wctime64`** | `<time.h>` or `<wchar.h>` |
8387

8488
For more compatibility information, see [Compatibility](../compatibility.md).
8589

docs/c-runtime-library/reference/ctime-s-ctime32-s-ctime64-s-wctime-s-wctime32-s-wctime64-s.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Convert a time value to a string and adjust for local time zone settings. These
1616
## Syntax
1717

1818
```C
19-
errno_t ctime_s(
19+
errno_t ctime_s( // See note in remarks section about linkage
2020
char* buffer,
2121
size_t numberOfElements,
2222
const time_t *sourceTime
@@ -31,7 +31,7 @@ errno_t _ctime64_s(
3131
size_t numberOfElements,
3232
const __time64_t *sourceTime
3333
);
34-
errno_t _wctime_s(
34+
errno_t _wctime_s( // See note in remarks section about linkage
3535
wchar_t* buffer,
3636
size_t numberOfElements,
3737
const time_t *sourceTime
@@ -124,6 +124,11 @@ The debug library versions of these functions first fill the buffer with 0xFE. T
124124

125125
By default, this function's global state is scoped to the application. To change this behavior, see [Global state in the CRT](../global-state.md).
126126

127+
> [!Note]
128+
> When you use Windows SDK version 10.0.26100.6901 and Visual Studio 2026 or later together, `ctime_s` and `_wctime_s` are no longer `static inline` (internal linkage). Instead, they're `inline` (external linkage).\
129+
> To return to the previous behavior, `#define _STATIC_INLINE_UCRT_FUNCTIONS=1` before including any CRT headers. By default, `_STATIC_INLINE_UCRT_FUNCTIONS` is set to 0.\
130+
> This change increases UCRT conformance with the C++ standard and improves compatibility with C++ modules.
131+
127132
### Generic-text routine mappings
128133

129134
| TCHAR.H routine | `_UNICODE` and `_MBCS` not defined | `_MBCS` defined | `_UNICODE` defined |

0 commit comments

Comments
 (0)