In this article we will write our first C program, compile it and run it. We’ll discuss what happens in the background of this process.
Hello World Program in C
All programs in C language start with a main function. It is an entry point for an application. Let’s see a simple Hello World program.
#include <stdio.h>
void main {
printf("Hello World");
}
This programs prints “Hello World” on screen.
In this simple program, we came across few things, which we will discuss now.
- We are using printf function to print “Hello World” on screen.
- Since, this printf function is defined within a library file called as stdio.h, we are including that library in the first line using a pragma called as #include.
- The printf function wrapped within main function.
- In C language, functions begin with a curly bracket and ends with curly bracket. Since, main is a function, it has to be wrapped with curly brackets.
Where to write this program?
You can choose any C or C++ compiler supported integrated development environment (IDE). You can even write this on a notepad and compile it through the command line.
But writing programs within an IDE has its own advantages. The IDEs may provide Intellisense support, you can quickly format your entire code etc.,
I prefer to use Code Blocks for quickly writing a program and run it. Code::Blocks is an open source, cross platform IDE to run C, C++ and Fortran programs.
The good thing with Code::Blocks is you don’t have to download and install the C compiler separately. Code::Blocks comes bundled with C compiler GNU GCC (MinGW/Cygwin). Other compilers that are included in the bundle are MS Visual C++ Free Toolkit 2003, Borland’s C++ Compiler 5.5, DigitalMars Free Compiler., OpenWatcom, Small Device C Compiler (SDCC).
You can learn more about this IDE from their wiki site.
Steps to Run C Program on Code::Blocks
- Open Code::Blocks IDE
- Select File => New => Empty file
- Paste the above code snippet
- Save the file with name HelloWorld.c at your desired location
- Press F9, to build and run the program
Compiling C Program
When you press F9, Code::Blocks performs two actions, compile your program and executing the program.
Compilation of the code compiles the source code to the object code. The object code is machine code, but in relocatable format, that means the absolute memory addresses are not yet defined in the instruction set. You can find this file with extension “obj”. For example, HelloWorld.obj.
Compilation also verifies the semantics of C programming language and reports if there are any violations as compilation errors.
As part of the building of the file, Linker links this object code with functions in the standard library and the actual memory addresses are mapped to create an executable file with extension “exe”. In our case, the output of the linking process is HelloWorld.exe.
You can now execute this executable file in any machine without the need of source code and compilation steps. However, the executable file is specific to the operating system. That means, if you generate the EXE file on a Windows operating system, that EXE file can only run on Windows platforms, but not on Linux.
Run C Program on GCC via Command-line
The GNU Compiler Collection (GCC) is a collection of compilers.
If you would like to compile the C programs on command-line yourself, you can install GCC.
If you are running on Unix operating systems or its variants like Apple Mac OS or Linux, GCC most likely will be available. GSS is not designed to run on Windows platforms, however there is a distribution called MinGW which can run on Windows platforms. MinGW is a port to run GSS on Windows. This distribution was compiled by Stephan Lavavej. Code::Block internally use the same distribution to run C programs. You can download “MinGW” distribution from Nuwen website.
Steps to Compile and Run C program on GCC
- Once you downloaded MinGW distribution, extract it to C drive
- Open Command Prompt
- Change directory to MinGW directory
- Run set_distro_paths.bat file for setting up the GCC
- Check the version of GCC by typing,
gcc --version
- If it prints the downloaded version correctly, then gcc is successfully installed on your machine
- Now, switch to the directory where you saved HelloWorld.c program
- Let’s build the our program by typing,
gcc HelloWorld.c
- Now we can run this program by just by typing,
HelloWorld
- We can also increase the warning level by typing,
gcc HelloWorld.c --Wall