C Environment Setup

It is very easy to get started with C programming, in fact, you don’t even need to set up an environment to run C code there are lots of online compilers available out there in which we can write and compile our code within no time.

image from tutorend.com

Tutorend itself provides you with an amazing Online C Compiler you can start from here but setting up a local environment is very important to learn the basics like how C compilers work and how our code is compiled.

#Basic C environment

You need two things in your computer before starting to code in C

  1. Text Editor – Whre you will write your C code, for example, Notepad
  2. C compiler – Which compiles our C code and outputs an executable program, for example, GCC

For Text Editor, you are good to go with the pre-installed Text editor Notepad if you are using Windows

If you want to set up a C environment this way then you can click on this link for step by step process.

#Using an IDE

An Ide or Integrated Development Environment is an all-in-one Text Editor which has advanced features you can write and compile code from only one window by clicking on some buttons.

Some of the Ide that supports C / C++ are Codeblocks, Visual Studio Code, Eclipse, etc.

#Setting Up Visual Studio Code for C / C++

Visual Studio Code (VS Code) is one of the most popular IDE and has lots of features like Emmet, Intelligence, Terminal, and Extensions.

Lots of developers choose Visual Studio code to write C / C++ code because of its error highlighting feature.

To setup VS Code for C / C++ we need three things

  1. Visual Studio Code
  2. C compiler ( MinGW )
  3. C / C++ extension for VS Code

#Installing Visual Studio Code

If you haven’t already installed Visual Studio Code then you can download it from this link https://code.visualstudio.com/Download

Download the version which suits best your Operating System and follow the instruction to install it.

#Installing MinGW

MingW is one of the easiest ways to install a C compiler on your computer, follow this link https://sourceforge.net/projects/mingw/ and download the installer which will help you to install a compiler for C, C++, and Fortran.

After downloading the MinGW installer, follow these steps to set up the C compiler:

  1. Run the downloaded installer and click “Install” on the welcome screen.
  2. Choose a directory where you want to install MinGW. It’s recommended to keep the default directory (C:\MinGW).
  3. Select the components you want to install. For this tutorial, you only need to select “mingw32-base” and “mingw32-gcc-g++” under “Basic Setup.”
  4. Click “Apply Changes” in the “Installation” menu.
  5. Wait for the installation to finish.

#Adding MinGW to PATH

To use the installed C compiler from any location in the command prompt, you need to add it to your system’s PATH variable. Follow these steps:

  1. Open the start menu and search for “Environment Variables.”
  2. Click on “Edit the system environment variables.”
  3. In the “System Properties” window, click on the “Environment Variables” button.
  4. Under “System Variables,” find and select the “Path” variable, then click “Edit.”
  5. Click “New” and add the path to the “bin” folder inside the MinGW installation directory (usually
  6. C:\MinGW\bin).
  7. Click “OK” on all the open windows to save the changes.
  8. Now you should be able to use the “GCC” command from the command prompt to compile C programs.

#Installing the C/C++ Extension for Visual Studio Code

To enable support for C/C++ in Visual Studio Code, you need to install the C/C++ extension provided by Microsoft. Follow these steps:

  1. Open Visual Studio Code.
  2. Click on the Extensions icon in the sidebar or press Ctrl+Shift+X to open the Extensions view.
  3. Search for “c++” in the search bar.
  4. Find the “C/C++” extension by Microsoft in the search results and click “Install.

Now that you have installed the necessary tools, you need to configure Visual Studio Code for C/C++ development. Follow these steps:

  1. Open Visual Studio Code.
  2. Create a new folder for your C project and open it in Visual Studio Code.
  3. Create a new file with a “.c” extension (e.g., “main.c”) to start writing your C program.
  4. Press Ctrl+Shift+P to open the Command Palette and type “C/C++: Edit Configurations (UI)” then press Enter.
  5. In the “C/C++ Configurations” tab, set the “Compiler path” to the path of the MinGW “gcc” executable (usually C:\MinGW\bin\gcc.exe).
  6. Save the settings and close the configuration tab.
  7. Now Visual Studio Code is configured for C development. You can write your C program, and the IDE will provide error highlighting, code completion, and other useful features.

To compile and run your C program, you can use the integrated terminal in Visual Studio Code:

  1. Press Ctrl+ to open the integrated terminal.
  2. Navigate to your project folder using the “cd” command.
  3. Compile your C program using the “gcc” command, followed by the input file name and the “-o” flag with the output file name (e.g., gcc main.c -o main).
  4. Run the compiled program by typing its name in the terminal (e.g., ./main on Linux/macOS or main on Windows).

Now you’re all set to start writing and running C programs using Visual Studio Code as your IDE.