A Quick Tip Before We Start: How to Copy and Paste in this Guide
- To Copy from this guide: Use your mouse to highlight the text or command you need. Press and hold the Ctrl key, then tap C on your keyboard.
- To Paste into a text file: Click where you want the text to go, hold Ctrl, and tap V.
- To Paste into a Command Line/Terminal window: Terminals are old-school and
Ctrl + Vdoesn't always work! The easiest and safest way to paste into a black terminal window is to Right-Click anywhere inside the terminal window with your mouse. A menu will appear where you can click Paste (in some terminals, simply right-clicking once will automatically paste the text).
Before compiling code, you need a place to write it. VS Code is a highly popular, free, and customizable code editor.
- Go to the official VS Code website: code.visualstudio.com.
- Click the Download for Windows button.
- Open the downloaded installer file and accept the license agreement.
- Keep clicking Next. When you reach the "Select Additional Tasks" screen, check the boxes for:
- Add "Open with Code" action to Windows Explorer file context menu
- Add "Open with Code" action to Windows Explorer directory context menu
- Click Install and then Finish.
Windows 11 does not come with a C/C++ compiler pre-installed. We will install the GCC compiler using a tool called MSYS2.
- Go to the MSYS2 website:msys2.org.
- Download the installer (look for a button like msys2-x86_64-latest.exe).
- Run the installer. Leave the installation folder as the default (
C:\msys64) and keep clicking Next until it finishes. - Ensure Run MSYS2 now is checked and click Finish. A black terminal window will open.
- Now, you need to update the system.
- Highlight the following command and copy it (
Ctrl + C):pacman -Syu
- Go to the black MSYS2 terminal window. Right-click anywhere inside the window and select Paste (or just click your right mouse button once to paste).
- Press Enter on your keyboard.
- (If it pauses and asks to proceed, type the letter
Yand press Enter. If the terminal closes itself after updating, open "MSYS2 MSYS" from your Windows Start menu to continue).
- Highlight the following command and copy it (
- Next, install the actual C and C++ compiler toolchain.
- Highlight and copy (
Ctrl + C) the following command:pacman -S --needed base-devel mingw-w64-ucrt-x86_64-toolchain
- Right-click inside the terminal to paste it, then press Enter.
- Highlight and copy (
- It will ask which packages to install. Simply press Enter to install all of them.
- When asked to proceed with the installation (
Proceed with installation?[Y/n]), type Y and press Enter. Wait for the download and installation to complete.
For your computer to recognize the compiler from any folder, you need to add it to your Windows Environment Variables.
- Click the Windows Start Button and search for Edit the system environment variables. Click the top result.
- A "System Properties" window will open. Click the Environment Variables... button at the bottom right.
- In the bottom section titled "System variables", scroll down until you find the variable named Path. Click on it to highlight it, then click Edit....
- Click the New button on the right side.
- Type the following text exactly as shown:
C:\msys64\ucrt64\bin - Click OK on all three windows to save your changes and exit.
Let's make sure Windows 11 can see your new compiler.
- Click the Windows Start Button, type cmd, and open the Command Prompt (another black terminal window).
- Type the following command and press Enter:
gcc --version
- Type the following command for C++ and press Enter:
g++ --version
If both commands return text stating the version of GCC installed, congratulations! Your compiler is set up. (If it says "command not found," restart your computer and verify you typed the Path correctly in Phase 3).
- Open Visual Studio Code.
- On the left-hand sidebar, click the Extensions icon (it looks like four squares).
- In the search bar at the top left, type C/C++.
- Find the extension named C/C++ published by Microsoft and click Install.
- Create a new folder on your Desktop and name it
CodingProjects. - Open VS Code. Go to File > Open Folder... and select the
CodingProjectsfolder. - In the VS Code Explorer on the left, click the New File icon (a piece of paper with a plus sign) and name the file
hello.c. - Copy (
Ctrl + C) the following C code from this guide. Click inside the emptyhello.cfile in VS Code, and paste it using Ctrl + V:#include <stdio.h> int main() { printf("Hello, World from C!\n"); return 0; }
- Save the file by pressing Ctrl + S.
- Open the VS Code integrated terminal by clicking Terminal > New Terminal at the top menu. A terminal panel will open at the bottom of the screen.
- Tell the compiler to turn your text into an executable program. Copy the following command, then Right-Click inside the terminal at the bottom of VS Code to paste it, and press Enter:
gcc hello.c -o hello.exe
- Run your newly created program by copying, pasting (Right-Click), and pressing Enter on this command:
Output:
.\hello.exe
Hello, World from C!
- In the same VS Code folder, create another new file and name it
main.cpp. (The.cppextension tells the editor it is a C++ file). - Copy (
Ctrl + C) the following C++ code, click inside your emptymain.cppfile, and paste (Ctrl + V):#include <iostream> int main() { std::cout << "Hello, World from C++!" << std::endl; return 0; }
- Save the file by pressing Ctrl + S.
- In the same terminal at the bottom of VS Code, tell the compiler to compile your C++ code. Copy, Paste (Right-Click), and press Enter:
g++ main.cpp -o main.exe
- Run your C++ program by copying, pasting (Right-Click), and pressing Enter:
Output:
.\main.exe
Hello, World from C++!
You have successfully learned how to navigate the command line, copy and paste into terminals, and set up your Windows 11 machine for C and C++ programming. Happy coding!