Before we learn the basic building blocks of the C language, let’s take a look at a minimal C program structure, which can be used as a reference in the following sections.
The C program mainly consists of the following:
· Preprocessor directive
· Statement & expression
Let’s look at a simple code that can output the word “Hello World”:
Next we explain the above procedure:
1. The first line of the program #include <stdio.h> is a preprocessor directive that tells the C compiler to include the stdio.h file before actually compiling.
2. The next line int main () is the main function, the program from here to start.
3. The next line /*…*/ will be ignored by the compiler, where the contents of the program will be placed. They are called annotations of the program.
4. The next line, printf (…), is another function that is available in C and displays the message “Hello, World!” On the screen.
5. The next line returns 0; terminates the main () function and returns the value 0.
Let’s take a look at how to save the source code in a file and how to compile it and run it. Here are the simple steps:
1. Open a text editor and add the above code.
2. Save the file as hello.c .
3. Open the command prompt and go to the directory where the saved file is located.
4. Type gcc hello.c , enter the carriage return, compile the code.
5. If there is no error in the code, the command prompt jumps to the next line and generates the a.out executable.
6. Now, type a.out to execute the program.
7. You can see “Hello World” on the screen .
Make sure that your path already contains the gcc compiler and that you are running it in the directory containing the source file hello.c.