DEV Community

Cover image for Setup up environment for C Programming
Rishabh Kumar
Rishabh Kumar

Posted on • Originally published at codewithrish.com

Setup up environment for C Programming

Requirements

To run a C Program on your pc you need 2 things.

  • Text Editor (Notepad, Notepad++, Sublime-Text-Editor)
  • C Compiler (Borland Turbo C, Tiny C Compiler, Portable C Compiler, GCC, Clang) GCC is most famous we will use GCC.
  • IDE (Optional but recommended we will use Visual Studio Code)

Installing GCC compiler

There are different ways to install GCC compiler.

First Method

  1. Go to the official website of MinGW-w64 Link
  2. Click MSYS2 or directly go to Link.
  3. Download the installer
  4. Install it with the default option
  5. It will give you a console now run the following commands
// Update the package database and base packages
`pacman -Syu`
// Run "MSYS2 MSYS" from Start menu. Update the rest of the base packages
`pacman -Su`
// Now MSYS2 is ready for you. You will probably want to install some tools and the mingw-w64 GCC to start compiling
`pacman -S --needed base-devel mingw-w64-x86_64-toolchain`
Enter fullscreen mode Exit fullscreen mode

Second Method

  1. Go to the official website of CodeBlock
  2. Clicks Downloads or direct Link
  3. Now click on Binary releases or direct Link
  4. Download codeblocks-20.03mingw-setup.exe using FossHUB Link (it's faster)
  5. Install it with default options.

Now if you open your cmd and type gcc it will still not be able to find that compiler you need to set the environment variable in order to access it globally.

Setting Environment Variable

Now we will copy the compiler path and set it in out system environment variables.

  • Default path for first method : C:\msys64\mingw64\bin
  • Default path for second method : C:\Program Files\CodeBlocks\MinGW\bin

  • Right click on This PC or My Computer select properties. you will get following screen (Windows 11)

Screenshot 2022-01-23 141811.png

  • Click advance system settings on older windows this option will be on left.
  • Now click Environment Variables

Screenshot 2022-01-23 142036.png

  • Now in the System variables select path variable and press edit.

Screenshot 2022-01-23 142335.png

  • Click New and paste you copied path and press ok.

Screenshot 2022-01-23 142654.png

Your environment variable is set now you can run gcc command on your whole PC.

Download and Install Visual Studio Code

We will use VS Code instead of simple notepad to write code. It's better and made to write code easily. Simple go to the official website and download and install with default options. Just remember to check Open With Code option while installation it's really helpful.

Install some extensions for better experience

Press Ctrl+Shift+X in VS Code to search for extentions. Install these extensions

  • C/C++ by Microsoft
  • Prettier - Code formatter

Click gear icon on bottom left and select settings. Search for mouse wheel and tick the checkbox saying Editor: Mouse Wheel Zoom

Screenshot 2022-01-23 143817.png

Now you are all set for writing your first code in C

Write and run your first code.

  • Create folder on Desktop name is Learning C
  • Drag this folder in VS Code
  • Create a file named hello.c and paste code written below
#include <stdio.h>

int main()
{
    printf("Hello World from CodeWithRish!");
    return 0;
}

Enter fullscreen mode Exit fullscreen mode

Click terminal in options and press new terminal.
In terminal write gcc hello.c it will generate and executable a.exe if everything is setup correctly. Now type .\a.exe it will give Hello World from CodeWithRish! this output.

Hope everything is setup correctly if still there is some problem drop a comment I'll definitely help you.

Top comments (0)