Coding

How to Code in C: Your First Program, Compile and Run

To code in C, install a compiler, save a few lines in a file ending in .c, compile it into an executable, and run that executable. With GCC the whole loop is gcc hello.c -o hello followed by ./hello, and this article walks through it on Windows, macOS and Linux.

C is a compiled language. You do not run your source file directly the way you run a Python script. A compiler translates the text into a program your operating system can execute, and you run that program. Learning to code in C therefore starts with two things: a compiler and a text editor.

What you need

  • A text editor. Notepad, VS Code, Vim, anything that saves plain text. The file must end in .c, not .txt.
  • A C compiler. The common ones are GCC, Clang and Microsoft's MSVC. On Linux and macOS you normally install GCC or Clang through your package manager or the platform's developer tools. On Windows, the Visual Studio Build Tools (the Desktop development with C++ workload) provide MSVC.
  • A terminal. You will type the compile command there. On Windows with MSVC you must use a developer command prompt, explained below.

Write your first C program

Create a folder for practice files, open a new file called hello.c, and type this:

#include <stdio.h>

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

Each line has a job:

  • #include <stdio.h> pulls in the declarations for the standard input/output library, which is where printf comes from. Without it the compiler does not know what printf is.
  • int main(void) is the entry point. Every C program starts running at main. The int means it returns a whole number to the operating system, and void means it takes no arguments. The other standard form is int main(int argc, char *argv[]), which receives command-line arguments.
  • printf("Hello, World!\n"); prints text. \n is a newline. Every statement in C ends with a semicolon.
  • return 0; reports success. Zero (or the constant EXIT_SUCCESS) means the program finished normally, and EXIT_FAILURE means it did not. Since C99, reaching the closing brace of main without a return behaves as if you had written return 0;, but writing it out is clearer for a beginner.

Compile and run with GCC or Clang (Linux, macOS, MSYS2)

From the folder that contains hello.c:

gcc hello.c -o hello
./hello

The -o option names the output file. According to the GCC documentation, if you leave it out, the executable is called a.out. The second line runs the program. Example output:

Hello, World!

Clang accepts the same command with clang in place of gcc. On Windows, an executable built by a GCC-based toolchain is usually named hello.exe and you run it with .\hello in PowerShell or hello in Command Prompt.

Compile and run with MSVC (Windows)

Microsoft's documentation is explicit that you cannot use MSVC in a plain command prompt. Open the Start menu, find the Visual Studio folder, and choose Developer Command Prompt for VS 2022 (the name changes by version). That shortcut sets the paths to the compiler, headers and libraries. Then:

cd C:\work\hello
cl hello.c
hello

The compiler writes hello.exe next to the source file. If you see 'cl' is not recognized as an internal or external command, you are not in a developer command prompt, or the C++ workload did not install correctly. MSVC treats files ending in .c as C and files ending in .cpp as C++, so keep the .c extension.

Turn on warnings from day one

A C compiler will accept many programs that are almost certainly wrong, such as using a variable you never set. Warnings catch a lot of that, and they cost nothing. With GCC or Clang:

gcc -Wall -Wextra hello.c -o hello

GCC's documentation describes -Wall as enabling "all the warnings about constructions that some users consider questionable", and -Wextra as enabling "some extra warning flags that are not enabled by -Wall". With MSVC, Microsoft recommends the /W3 or /W4 warning level, for example cl /W4 hello.c. Treat a warning in your own code as a bug to fix, not noise to ignore.

Choosing a C standard

C has been revised several times: C99, C11, C17 and C23. You can pin one with -std=c11 or -std=c17 on GCC and Clang, or /std:c11 or /std:c17 on MSVC, which also needs a recent Windows SDK for C11 and C17 support. GCC's documentation says that for C code, its default is the GNU dialect of ISO C23. For learning, the default is fine. Pin a standard only when you need your code to build the same way on different machines.

Common beginner mistakes

  • Forgetting to recompile. Editing hello.c does not change hello. Run the compile command after every edit, then run the new executable.
  • Missing semicolons or braces. The compiler reports the line where it noticed the problem, which is sometimes the line after the real mistake. Look at the previous line too.
  • Saving as hello.c.txt. Windows hides known extensions by default. Turn on file name extensions in File Explorer, or save from your editor with the type set to all files.
  • Reading input unsafely. Beginners often reach for scanf and run into surprises with strings and return values. See scanf in C: syntax, return value and pitfalls before you build anything interactive.
  • Printing with the wrong format. printf needs a matching specifier for each value, such as %d for an int. The table in C format specifiers for printf and scanf lists them.

A second program: variables and a loop

Once hello world runs, change it to do a little work. This program adds the numbers 1 to 10:

#include <stdio.h>

int main(void)
{
    int total = 0;

    for (int i = 1; i <= 10; i++) {
        total += i;
    }

    printf("Sum: %d\n", total);
    return 0;
}

Compile it the same way. Example output:

Sum: 55

Here int total = 0; declares a variable and gives it a value. In C, a local variable that you do not initialise holds an indeterminate value, so always set it. The for loop has three parts: a starting statement, a condition checked before each pass, and a step run after each pass. The details are in C for loop: syntax and examples.

What to learn next

A sensible order for the core of the language is:

  1. Types, variables, operators and printf.
  2. Conditions and loops (if, switch, for, while).
  3. Functions, so you can split a program into named pieces.
  4. Arrays and strings. C strings are arrays of char ending in a zero byte, which is the source of many classic bugs.
  5. Pointers. They are the feature that makes C feel different from most languages, and they are the topic most worth taking slowly. Start with pointers in C explained with examples.
  6. Structs and dynamic memory with malloc and free.

If you already know another language, Python vs C: the differences shows what changes when you move to a compiled language with manual memory management.

If something does not work

Read the first error message, not the last. One mistake can trigger a chain of follow-up errors, and fixing the first one often clears the rest. Check that the file name in your compile command matches the file on disk, that you are in the right folder (dir on Windows, ls on macOS and Linux), and that the extension is really .c. If the compile succeeds but the program misbehaves, add printf lines to show the values of your variables and compare them with what you expected. Small programs, compiled often, with warnings on, are the fastest way to learn C.

Frequently asked questions

Do I need an IDE to code in C?
No. A text editor and a compiler on the command line are enough, and that is the setup Microsoft's and GCC's own documentation use. An IDE adds a debugger and project management, but the compile step underneath is the same command.
Why does my C program print nothing?
Usually the output is there but the window closed, or the program was never rebuilt after you edited it. Run it from a terminal so the window stays open, and run the compile command again after every change. Also check that your printf string ends with a newline, because output that ends without one may not appear until the program exits.
What is the difference between C and C++?
They are separate languages that share a lot of syntax. MSVC treats files ending in .c as C and files ending in .cpp as C++, so name your file hello.c when you want C. C++ adds classes, templates and a larger standard library on top of a C-like core.
Which C standard should I use?
For learning, the default your compiler uses is fine. If you want to pin one, GCC and Clang accept -std=c11 or -std=c17, and MSVC accepts /std:c11 or /std:c17. GCC's documentation states that the default for C code in its current release is GNU C23.

Sources