Here are the steps for getting started with basic C development:
VS Code
Visual Studio Code is the go-to editor for beginner level software engineers. It has a large ecosystem of plugins and themes. Download VS Code by visiting the official website.
Run the installer.
MinGW
This is a Windows distribution containing the GCC compiler. VS Code does not come with built-in support for C compilation so we use MinGW to get access to the compiler. Download the latest MSVCRT runtime zip file from the website.
Unzip the file and place the contents in your C drive. We now need to add it to PATH. By doing this, you can access any program e.g. gcc.exe within the folder without actually being inside of that folder - you can run GNU C Compiler from anywhere.
Add the folder to PATH by doing the following:
- Copy the full path to the mingw/bin folder you added to your C drive
- Search for environment variables via Windows+S
- Choose the option ‘Edit the system environment variables’
- A window named System Properties will appear - choose ‘Environment Variables…’ at the bottom of this window
- Under the ‘System variables’ section for the new ‘Environment Variables’ window, select ‘Path’ and then choose ‘Edit…’
- A window named ‘Edit environment variable’ will appear, select ‘New’
- Paste the path to the mingw/bin folder into the box and press ‘OK’
- Select ‘OK’ until all of the previously mentioned windows disappear
Code Runner extension
- Open VS Code and navigate to the ‘Extensions’ tab or press Ctrl+Shift+X
- Search for the Code Runner extension by Jun Han and install it
- Access the settings by pressing Ctrl+,
- Expand ‘Extensions’ and choose ‘Run Code configuration’
- Under ‘Run In Terminal’, turn on ‘Whether to run code in Integrated Terminal.’
C/C++ Extension Pack
Find and install the plugin of the same name.
Running your first C program
- Create a file called ‘hello.c’
- Paste the following code into the text area:
#include <stdio.h> int main(void){ // this is a comment printf("Hello world!\n"); return 0; }
hello.c Copy - Open the terminal by pressing Ctrl+Shift+`
- Compile your code by typing the following:
gcc hello.c -o hello
- Run the executable file by typing the following:
./hello.exe
The ./ tells your computer to look in the current folder for the file and hello.exe is the name of the program you just created.
You can also run your code by pressing Ctrl+Alt+N if you have installed the Code Runner extension
Congrats!! You’ve made your first piece of software! It’s time to put “Junior Software Engineer” on your LinkedIn profile.
Checking the exit status of a program
The exit status is the return value of the main function. On Windows, run:
echo %ErrorLevel%
On Linux and Mac, run:
echo $?
Adding your new program to PATH
We can add the folder containing our new program to PATH in order to access it from anywhere:
- Open a command prompt typing ‘cmd’ and Enter after pressing Windows+S
- Type ‘dir’ and press Enter - ‘dir’ is a command for listing the files in the current directory
- Type ‘gcc’ and Enter - gcc is the program we just used to compile our program; notice how it is accessible from the current folder despite being in the mingw/bin folder
- Type ‘hello’ and press Enter - you will see a message like ‘hello’ is not recognized as an internal or external command, operable program or batch file; this is because your program is within another folder so your computer doesn’t know where to look for it; we can add it to PATH so that it does
- Copy the path to the folder in which you placed your hello.c file
- Rinse and repeat the steps you did to add the mingw/bin folder to PATH
- Type ‘hello’ in a new command prompt window - we need a new command prompt window because the older window will not have the updated environment variables including our new version of PATH
- You should now see ‘Hello world!’ being printed in the terminal window