Setup up environment for C Programming
Tags: c, codenewbie, beginners, programming
Published: 2022-01-23 09:39:53

## 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](https://www.mingw-w64.org/)
2. Click MSYS2 or directly go to [Link](https://www.msys2.org/).
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`
``` 
### Second Method

1. Go to the official [website](https://www.codeblocks.org/) of CodeBlock
2. Clicks Downloads or direct [Link](https://www.codeblocks.org/downloads/)
3. Now click on Binary releases or direct [Link](https://www.codeblocks.org/downloads/binaries/)
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](https://cdn.hashnode.com/res/hashnode/image/upload/v1642927753540/ZTh9HRd0f.png)
- Click `advance system settings` on older windows this option will be on left.
- Now click `Environment Variables`

![Screenshot 2022-01-23 142036.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1642927944355/Tl8Ikjah9.png)
- Now in the System variables select path variable and press edit.

![Screenshot 2022-01-23 142335.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1642928097856/Trk2hkI-b.png)
 
- Click New and paste you copied path and press ok.

![Screenshot 2022-01-23 142654.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1642928232714/DFcudUxXK.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](https://code.visualstudio.com/) 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](https://cdn.hashnode.com/res/hashnode/image/upload/v1642928908724/Gw7-uG5yV.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;
}

``` 
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.