Welcome - are you ready to create your first C program?
This document details the process of getting started creating console-based C programs. Console-based programs are quite simple - they take input from the keyboard (or a file stored on disk) and they produce output to the text-based console window. You will need two things to create C programs: a text editor to write the source code for the program and a compiler to convert the source code to an executable file so the program can be run (on Windows, executable files have a ".exe" extension). We will use Microsoft's Visual Studio Community software which provides both a powerful code editor and a C compiler (as these are packaged in the same software, this is called an integrated development environment, or IDE).
To get started, we need to install Visual Studio Community. We will then look at two different ways to develop simple console-based C programs:
OK, let's get started by installing Visual Studio Community...
There are several different versions of Microsoft's Visual Studio - the Community version is free and includes all of the necessary libraries and tools that we need to develop C programs. You can google "Visual Studio Community" to find out more, or go directly to the relevant page on Microsoft's website:
Make sure "Windows" is selected and click on the "Download VS Community 2017" button as shown below:
Once you click the "Download VS Community 2017" button, you will be prompted to save a file called "vs_community_<some numbers>......exe" - save this file somewhere convenient such as your Desktop:
Just double-click on this downloaded file ("vs_community_<some numbers>......exe"). This will launch the installer program (you may be prompted to confirm to "Run" this program), in which case just click "Run":
The installation process will then begin - click "Continue" when prompted:
In the first stage of the installation a number of files will be downloaded to your computer. You will just need to be patient until this stage is complete:
In the second stage of the installation you will be shown the configuration options below. There are several tabs along the top - you only need to look at the default tab which is called "Workloads" as shown below.
IMPORTANT: Make sure that you click the "Desktop development with C++" option as shown below:
Once you select the "Desktop development with C++" option, you will see a "Summary" shown on the right hand side of the window as illustrated below. Although some of these components are listed under "Optional", it is simplest to accept all of the default options as highlighted. It is particularly important that you ensure "VC++ 2017 Version ... tools" and "Windows 10 SDK" are both selected. Without these you will not be able to compile C programs!
IMPORTANT: Make sure that the "VC++ 2017 Version ... tools" and "Windows 10 SDK" options are both selected before continuing:
When you are ready you can then click the "Install" button in the bottom right corner of the window. This will start the installer once again, and the necessary components will be added:
Once this is finished, you may be prompted to restart your computer (click "Restart"):
After your computer has restarted, you should find that Visual Studio Community is now installed. To check that the installation was successful, you can try launching Visual Studio. Please note, there is no need to run the installation program again (this is the "vs_community_<some numbers>......exe" file that you downloaded from Microsoft's website). This installation program is no longer needed. Instead, to launch Visual Studio, you should select it from the Start menu as shown:
As this is the first time you have launched Visual Studio, you will be asked to "Sign in" (just ignore this if you like by clicking "Not now, maybe later"), then you will be asked to select a "theme" (the default theme called "Blue" is just fine) and then there will be short delay while the environment is configured for the first time:
And finally, you should see the Visual Studio Community start page! Congratulations, you can now start creating your first C program!
You have two options for the process of developing your C programs, and these are described below.
Great, now that Visual Studio Community is installed, you have two options for developing and running C programs on Windows. The first option involves using any text editor you like to write your source code, and using the "cl" command within the Developer Command Prompt to compile your code. The second option involves using Visual Studio Community as the development environment, and creating a Visual Studio "project" to organise your files.
Both options are described next...
Editing the source code
We can write our source code in any text editor we like. In this example, I am using a text editor called TextPad, and you can see in the diagram below that I have created a C source file called "example.c" inside a folder called "MyFirstProgram" which is inside a directory called "paul" on the "C:" of my computer:
Compiling and running the program
To compile our program, we are going to run the Visual Studio command line compiler, called "cl". We run this command from an environment called the "Developer Command Prompt". To launch the Visual Studio "Developer Command Prompt", type "Developer" into the search box in the Start menu and you should see an option "Developer Command Prompt for VS 2017" appear as shown. Click on this program:
You should see a new window open - and the title of the window should be "Developer Command Prompt for VS 2017". This is the window into which we will type the commands to compile and execute our C program.
From the Developer Command Prompt, you change directories by typing:
You can change drives by typing the drive letter on its own (for example, to switch to the "E" drive or "E:"):
You can list all of the files in the current directory by typing:
And finally, you can compile your source file with the command:
In this case, "example.exe" is the name of the executable file that will be created if your code compiles successfully. In the screenshot below, you can see that a new file called "example.exe" was created in the same directory as the source file.
To run the executable, you simply type:
which means: "execute the program named "example" which is in the current directory
These commands are illustrated below:
And that's it - you can now edit the source file, re-compile the code, and run the program again!
Visual Studio Community is an integrated development environment. This means we can write our source code, compile the code, run the executable and view the output all from within the Visual Studio program. We are now going to look at how to do this. Visual Studio organises programs into what it calls "Projects". So we are going to create a new Visual Studio project in a folder called "paul" on the "C:" of our computer. As you can see at the moment, this folder is empty:
First, we launch Visual Studio Community from the Start menu:
And we create a new project by selecting "File > New Project..."
Make sure you carefully select the correct template. This should be "Windows Desktop Wizard" which can be found by selecting "Windows Desktop" underneath the "Visual C++" template.
As you can see in this example, we have called the project "ExampleProject", and we have selected the location to be the folder "C:\paul". Also, we have left the option to "Create directory for solution" unchecked:
We are then shown the Windows Desktop wizard which allows us to configure our new project. We must make sure that the Application type is Console Application (.exe). We must also create an "empty" project (and leave "Export Symbols", "Precompiled Header" and "Security Development Lifecycle checks" all unchecked). Once this is correct, then click on "OK"
The project will then be created, and you will be shown a graphical representation of the project. This is illustrated below. Because the project is empty, the first thing we must do is add a source file:
We can add a source file by right-clicking the "Source files" item (in the Solution Explorer pane) and then selecting "Add > New Item..."
When we create our source file, it is very important that we give it the correct extension (i.e. suffix) for a C file. This must be ".c":
We can then edit our source file by writing code directly in the Visual Studio Community code editor:
When we are ready to compile our source file, we can select "Build Solution" from the "Build" menu:
It is useful to have the "Output" pane open - in the diagram below, the "Output" pane displays the status of the compilation process. In this example, the compilation succeeded with no errors:
After our code has compiled successfully, we can then run our program. The best way to do this is to select "Start without debugging" from the "Debug" menu. This "Start without debugging" option will add a pause once our program has executed, allowing us to view the output (until we press a key) before the window closes:
And, if all has gone well, we will see the output from our program in a new command window:
It is really very useful to have an understanding of the files that Visual Studio creates and where these files are stored on disk. If you have followed the suggested organisation described here, then the folder in which we created the project (which, in this example, was "C:\paul") will contain just a single folder with the name of the project.
Inside this "ExampleProject" folder, there will be several files, including our source file, "example.c". One of the folders inside the "ExampleProject" is called "Debug".
The "Debug" folder is where the actual executable file for our program is created by the compiler. The diagram below illustrates the organisation on disk.
Now, if you want to make changes to your program, you return to the editor and modify the source file. To run the program again, you must recompile it ("Build solution") and then run it ("Start without debugging").
And that's all there is to it! Good luck!