Getting started with C programming - Hello world
-
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. You can also check the
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.
Press Win+R, then type cmd
into the box.
Quick command line guide:
- The command line lets you browse your files and run programs. To find out what's in the current folder, type
dir
. - Type
cd foldername
to enter a folder,cd ..
to exit folder. Press Tab to auto-complete folder/file names. - Press Up and Down arrow keys to browse previously entered commands.
- Type the name of a program to run that program. Anything you type after the program name will be given to that program, for example
compiler.exe mycode.c
will run compiler.exe and give the text "mycode.c" to it. Note:.exe
and.bat
are optional, for example typingcompiler
will runcompiler.exe
orcompiler.bat
if such files are found. - Press Ctrl+C to exit whatever program is running, it will be important when you run your own program.
Install the compiler
You need a compiler to turn your code into a program. We'll use GCC because it's the simplest 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 or cookies rquired):
https://nuwen.net/mingw.html (mingw-XXX.exe)
On the command line, 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 command line command like gcc main.c -o testprogram.exe
, but a better way is to create a build file 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. 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 command line, 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 command line. 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 pause/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. pause exit ) rem If successfully compiled without errors, run the program. testprogram.exe
This is not ideal if you're using the command line because exit
will probably close the CMD window that you were using. If you're compiling straight from a text editor that is capable of showing you the error messages, consider removing the pause
since it may cause the script to get stuck. The ideal build script depends on your method of compiling the program.