Installing Visual Studio Code
To install Visual Studio Code on your computer, navigate to the Visual Studio Code download page and download and install the appropriate version for your operating system.
Once you have Visual Studio Code installed, install the C/C++ extension from the marketplace. You can install the extension by navigating to the Extensions tab on the left-hand-side of the Visual Studio Code window.
Windows-Specific Instructions
NOTE:If you are running Mac OSX or Linux, you can skip these instructions.
If you are running Windows, you’ll also need to install gcc
.
- Install Mingw-w64 via the SourceForge website. Click Mingw-w64 to download the Windows Mingw-w64 installer.
- Run the installer.
- For Architecture select
x86_64
and then select Next. - Next again to use the default installation folder and install MinGW.
- Add the path to your Mingw-w64
bin
folder to the WindowsPATH
environment variable by using the following steps:- In the Windows search bar, type ‘settings’ to open your Windows Settings.
- Search for Edit environment variables for your account.
- Choose the
Path
variable and then select Edit. - Select New and add the Mingw-w64 path to the system path. The exact path depends on which version of Mingw-w64 you have installed and where you installed it. If you used the settings above to install Mingw-w64, then add this to the path:
C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin
. - Select OK to save the updated PATH. You will need to reopen any console windows for the new PATH location to be available.
- Test your installation by opening up
Windows Powershell
and typing the commandgcc --version
. If the command succeeds, you are good!
Compiling a C Program
We will now try creating and compiling a C program. In Visual Studio Code, create a file named hello.c
on your computer by navigating to File -> New File
and copy/paste the following code into it:
#include <stdio.h>
int main()
{
printf("Hello, World!\n");
return 0;
}
You will need access to a Terminal in order to compile and run your code. To open one in Visual Studio Code, click Terminal -> New Terminal
. You should be presented with a shell that allows you to type in textual commands.
$
Depending on your operating system, the prompt of the shell might be a >
or a $
. In any case, try typing gcc --version
to verify that the gcc
compiler is available. You should see something like the following:
$ gcc --version
gcc (Ubuntu 9.3.0-10ubuntu2) 9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.
Then you can try to compile your hello.c
file by typing:
$ gcc hello.c
This command executes the gcc
compiler on the hello.c
file which ought to produce an executable file named a.out
(a.exe
if you are on Windows). To execute your program, type:
$ ./a.out
Hello, world!
NOTE: If you are running Windows, you will probably need to type ./a.exe
instead of ./a.out
into the shell.
If this worked, you’re all set! You should be able to write C programs by creating files with a .c
extension, and then compiling them using gcc
like we did above.