How to Write Code in C Programming on Windows

Faraz

By Faraz -

Learn how to write code in C programming on Windows with this simple step-by-step guide. Perfect for beginners to start coding in C.


how-to-write-code-in-c-programming-on-windows.png

C programming is a popular language used in many applications, from operating systems to games. If you're new to coding, starting with C on Windows is a great choice. This guide will walk you through the steps to write your first C program on Windows, from setting up your environment to running your code. Let’s get started!

Step 1: Install a C Compiler

To write and run C code on Windows, you first need a C compiler. A compiler turns your C code into an executable program. One of the most popular free compilers is MinGW.

  1. Download MinGW: Go to the MinGW website and download the installer.
  2. Install MinGW: Run the installer and select the packages for the C compiler and development tools.
    mingw component
  3. Set Environment Path: After installation, add MinGW to your system’s PATH so you can run it from the command line. This can be done by:
    • Right-click on This PC or Computer and select Properties.
    • Click Advanced system settings > Environment Variables.
    • Under System variables, find the Path variable, select it, and click Edit.
    • Add the path to MinGW's bin folder (e.g., C:\MinGW\bin).
    • Click OK to save changes.

Step 2: Install a Code Editor

While you can write C code in any text editor, it's easier to use a code editor designed for programming. Visual Studio Code (VS Code) is a great option and is free to use.

  1. Download Visual Studio Code: Go to the VS Code website and download the installer for Windows.
  2. Install Visual Studio Code: Run the installer and follow the setup instructions.
  3. Install C/C++ Extension: Open VS Code and go to the Extensions view by clicking on the square icon on the sidebar. Search for “C/C++” and install the official extension by Microsoft.

Step 3: Write Your First C Program

Now that you have a compiler and editor set up, you can write your first C program.

  1. Create a New File: Open VS Code and create a new file by clicking File > New File. Save it with a .c extension (e.g., hello.c).
  2. Write the Code: Type the following simple C program into the file:
    #include 
    
    int main() {
        printf("Hello, World!\n");
        return 0;
    }
    This code includes the standard input/output library, defines the main function, and prints "Hello, World" to the screen.

Step 4: Compile the C Program

Once you've written your code, you need to compile it into an executable program.

  1. Open Command Prompt: Open the Command Prompt by pressing Win + R, typing cmd, and hitting Enter.
  2. Navigate to Your Code: Use the cd command to navigate to the folder where you saved your .c file.
    cd path\to\your\file
  3. Compile the Code: Run the following command to compile your code:
    gcc hello.c -o hello.exe
    This command tells the GCC compiler to compile hello.c and output the result as hello.exe.

Step 5: Run the Compiled Program

After compiling, you can run your program to see the result.

  • Run the Program: In the same Command Prompt window, type:
    hello.exe
  • See the Output: If everything is set up correctly, you should see the output "Hello, World!" displayed in the Command Prompt.

Conclusion

Congratulations! You've successfully written, compiled, and run your first C program on Windows. This is just the beginning of your journey into C programming. As you continue learning, you'll be able to create more complex programs and understand deeper concepts. Remember, practice makes perfect, so keep coding and experimenting with new ideas.

That’s a wrap!

I hope you enjoyed this article

Did you like it? Let me know in the comments below 🔥 and you can support me by buying me a coffee.

And don’t forget to sign up to our email newsletter so you can get useful content like this sent right to your inbox!

Thanks!
Faraz 😊

End of the article

Subscribe to my Newsletter

Get the latest posts delivered right to your inbox


Latest Post

Please allow ads on our site🥺