hello.c — the file extension must be .c) containing the following source code:
hello.c
Code: Select all
#include <stdio.h>
int main(void)
{
puts("Hello, World");
return 0;
}
Code: Select all
#include <stdio.h>
See more about headers.
Code: Select all
int main(void)
Code: Select all
{
…
}
Code: Select all
puts("Hello, World");
The string to be output is included within the parentheses.
"Hello, World" is the string that will be written to the screen. In C, every string literal value must be inside the double quotes "…".
See more about strings.
In C programs, every statement needs to be terminated by a semi-colon (i.e.

Code: Select all
return 0;
this example, we are returning the integer value 0, which is used to indicate that the program exited successfully.
After the return 0; statement, the execution process will terminate.
Editing the program
Simple text editors include vim or gedit on Linux, or Notepad on Windows. Cross-platform editors also include Visual Studio Code or Sublime Text. The editor must create plain text files, not RTF or other any other format.
Compiling and running the program
To run the program, this source file (hello.c) first needs to be compiled into an executable file (e.g. hello on
Unix/Linux system or hello.exe on Windows). This is done using a compiler for the C language.
See more about compiling
Compile using GCC
GCC (GNU Compiler Collection) is a widely used C compiler. To use it, open a terminal, use the command line to
navigate to the source file's location and then run:
Code: Select all
gcc hello.c -o hello
given by the argument to the -o command line option (hello). This is the final executable file.
We can also use the warning options -Wall -Wextra -Werror, that help to identify problems that can cause the
program to fail or produce unexpected results. They are not necessary for this simple program but this is way of
adding them:
Code: Select all
gcc -Wall -Wextra -Werror -o hello hello.c
To compile the program using clang you can use:
Code: Select all
clang -Wall -Wextra -Werror -o hello hello.c
Using the Microsoft C compiler from the command line
If using the Microsoft cl.exe compiler on a Windows system which supports Visual Studio and if all environment variables are set, this C example may be compiled using the following command which will produce an executable hello.exe within the directory the command is executed in (There are warning options such as /W3 for cl, roughly analogous to -Wall etc for GCC or clang).
Code: Select all
cl hello.c
Once compiled, the binary file may then be executed by typing ./hello in the terminal. Upon execution, the
compiled program will print Hello, World, followed by a newline, to the command prompt.