How to compile and run C program using command line in Windows

This is one of the most frequently asked question to me. Creating and compiling a C program using an IDE is like waving some magic wand. However, a beginner must know how to compile and run C programs using command line in Windows based operating system.

To create a C program using command line you need two basic software’s.

  1. A text editor (such as Notepad or Notepad++ ).
  2. A C compiler.

You must have C compiler installed on your computer before you proceed.

Read more - How to download, install and configure CodeBlocks with C compiler in Windows

How to create a C program using Notepad (Windows)?

  1. Open notepad.
    Hit windows button and type notepad in it.
    Alternatively, hit Win + R, type notepad and hit enter to open notepad.
  2. Type C source code in notepad. For now do not care about what you are typing just copy paste the source code. We will have in depth discussion on C program structure later.

    Copy and paste the below source in your notepad.

    #include <stdio.h>
    
    int main()
    {
        printf("Hello, Codeforwin!");
    
        return 0;
    }

    Write C program in notepad

  3. Click on File → Save As in the menu bar. Alternatively, hit Ctrl + S to open Save As dialog box.

    Click Save As to save C program

  4. Give some name to your first C program. Add .c extension at the end of your file name. Also be sure not to save this file as plain text file. Change the plain text file option from Save as type option to All files.

    Save C program with .c extension

How to compile and run a C program using command line?

Once you created your first C program. It’s time for real action. A program is worthless until it is compiled and executed.

Read more about - The C compilation process

Syntax to compile single C program using GCC

gcc <file-name.c> -o <output-file-name>

Syntax to compile multiple files at once using GCC

gcc <file-name1.c> <file-name2.c> ... <file-name-N.c> -o <output-file-name>

Syntax to run a C program

<output-file-name>

Example to compile and run above program

Before you compile and run the above C program. You must be in the same directory as of your C program. To test your program open command prompt and execute following commands.

gcc hellocodeforwin.c -o hellocodeforwin.c

hellocodeforwin

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>