Loading...

Setting Up C Environment and Running First Program

Before you start coding in C, you need to set up the development environment on your system. This includes installing a compiler and a code editor to write and run C programs.

1. Installing a C Compiler

To run C programs, you need a C compiler. Some popular compilers are:

  • GCC (GNU Compiler Collection) – For Windows, macOS, and Linux.
  • MinGW – GCC for Windows.
  • Turbo C++ – A classic C compiler (outdated but still used by some learners).
  • MSVC (Microsoft C Compiler) – Part of Microsoft Visual Studio.

Installing GCC Compiler

To install GCC, follow these steps based on your operating system:

For Windows:

  1. Download MinGW-w64 from https://www.mingw-w64.org/.
  2. Install MinGW and add the bin directory to the system’s PATH variable.
  3. Verify installation by running gcc --version in the command prompt.

For macOS:

  1. Open Terminal and install Xcode Command Line Tools:
  2. xcode-select --install
  3. Verify installation by running gcc --version.

For Linux:

  1. Open Terminal and install GCC:
  2. sudo apt update
    sudo apt install gcc
  3. Verify installation using gcc --version.

2. Choosing an IDE or Code Editor

You can write and run C programs using various Integrated Development Environments (IDEs) or any text editors(notepad, notepad++ etc):

  • Code::Blocks – Lightweight and beginner-friendly.
  • Dev-C++ – Classic C/C++ IDE.
  • Visual Studio Code – Requires C extension.
  • CLion – Advanced IDE by JetBrains.

Installing and Setting Up Code::Blocks

  1. Download Code::Blocks.
  2. Install the version with MinGW included.
  3. Open Code::Blocks, go to Settings → Compiler and ensure MinGW is detected.

3. Writing and Running Your First C Program

Step 1: Open a Text Editor or IDE

Open Code::Blocks, VS Code, or a simple text editor like Notepad++.

Step 2: Write a Simple C Program

Create a new file and write the following basic C program:

#include <stdio.h>

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

Step 3: Save the File

Save the file with a .c extension, e.g., hello.c.

Step 4: Compile and Run the Program

Using Code::Blocks:

  • Click on Build & Run.
  • The output will appear in the terminal window.

Using Command Line:

  1. Open the command prompt (Windows) or terminal (macOS/Linux).
  2. Navigate to the directory where the file is saved using cd command.
  3. Compile the program using GCC:
  4. gcc hello.c -o hello
  5. Run the program:
  6. ./hello

4. Common Compilation Errors and Fixes

Error Possible Cause Solution
gcc: command not found Compiler not installed or not in PATH Install GCC and set PATH variable
undefined reference to main Missing main() function Ensure int main() exists in the code
expected ';' before 'return' Missing semicolon Check for missing semicolons

Now that you have successfully set up your C environment and run your first program, you are ready to explore more complex topics such as variables, data types, operators, and control statements.

If you have any questions, feel free to ask. Thank you for reading!

Thankyou!