return;

Getting started with C programming - Hello world Windows -

There's 3 things you need to do: install a compiler → write some code → give the code to the compiler.

This page is for Windows Windows. You can also check the Linux Linux version.


Setup tools

Open command line (CMD)

You don't need to start using it, but the compiler must be started with a command line command so it's good to know the idea behind it. From now on I will refer to this as just "CMD".

Press Win+R, then type cmd into the box.

Quick command line guide:

Install the compiler

You need a compiler to turn your code into a program. We'll use GCC because it's easiest to start with and also works on Linux.

You'll need to install MingW (which has GCC for Windows):
https://sourceforge.net/projects/mingw/

Alternate unofficial version (no javascript/cookies/captchas rquired):
https://nuwen.net/mingw.html (mingw-XXX.exe)
(You can also download this package as separated components from components-XXX.7z)

On the CMD, type gcc --version to test if it works, you should get some information about the GCC version. You can also just type the full path to the compiler executable, for example: "C:/Users/Sun/MinGW/bin/gcc.exe" --version.

In order to run it simply by typing gcc, you need to add the path to gcc.exe into your PATH variable. If you install GCC with an installer, it will probably do this for you.

Search "windows PATH variable" from a search engine if you want to learn more.


Make a test program

Save the code below as "main.c":

#include <stdio.h>

int main () {
	printf("Bag of biscuits\n");
	return 0;
}

Now you just need to give the code to GCC. You can do it with a CMD command like gcc main.c -o testprogram.exe, but a better way is to create a "build script" that will do the commands for you, so you only need to type build.

Batch files (.bat) are little programmable scripts that work on Windows only, they can be easily edited with any text editor. The batch file is basically just a list of CMD commands, running it is mostly the same as typing the contents of the file into the CMD yourself. Other operating systems have their own equivalents, for example shell scripts (.sh) on Linux.

Save the below as "build.bat" into the same folder:

@ECHO OFF

rem Compile the code.
gcc main.c  -o testprogram.exe  && (
	rem If successfully compiled without errors, run the program.
	testprogram.exe
)

On CMD, navigate to the folder and type build, the build script will create your program and immediately runs it. Your program should print "Bag of biscuits" onto the CMD. If there's errors in your code file, GCC will print a bunch of error messages.

I highly recommend setting up some kind of keyboard shortcut that runs this build.bat file. Most text editors can do it.

You can also build your program by double clicking the .bat file, however if GCC finds errors in your code, you won't be able to read the error messages because the CMD window will close immediately. You may also consider:

Alternate build file

Instead of stacking commands inside parentheses, you can inverse the condition and exit the script.

@ECHO OFF

rem Compile the code.
gcc main.c  -o testprogram.exe  || (
	rem If GCC failed to compile your program, pause and then exit the script.
	exit /b
)
rem If successfully compiled without errors, run the program.
testprogram.exe

Personally I like it this way more. The ideal build script depends on your preferences and methods of compiling the program. For example if you're compiling by clicking the build script, consider adding a pause command directly above the exit command and at the very end of the script, that will keep the CMD window open so you can read the text.