C Program Structure

The C program structure is an important issue for us to find out what a c program contains, what sections it is divided into.

Before examining the basic building blocks of the C programming language, let's look at a simple C program structure so that we can take it as a reference in future sections.

Hello World Example

Program C consists mainly of the following sections –

  • Preprocessor Commands
  • Functions
  • Variables
  • Expressions
  • Comments
<stdio.h>#include

int main() {
   /* comment section */
   printf("Hello World!</stdio.h> n");
   
return 0;
}

Let's take a look at the various sections of the above program –

  • #include <stdio.h></stdio.h>  The first line of the program is a preprocessor command that tells the C compiler to add the stdio.h library before proceeding to the actual assembly.
  • The next line, int main(), is the main function in which program execution begins.
  • The next line /*…*/ is ignored by the compiler and placed to add annotations to the program.Therefore, such lines are called comments in the program.
  • The next line is printf(…) , another function in C that causes the message "Hello, Earth!" will be displayed on the screen when it is written here.
  • The next line is return 0; Ends the main() function and returns a value of 0.

Compiling and Running Program C

Let's see how to save the source code to a file and compile and run it:

  • Open a text editor and add the above-mentioned code to your text editor.
  • Save the file as hello.c.
  • Open a command prompt or terminal and navigate to the directory where you saved the file.
  •  Compile your code through the terminal by typing gcc hello.c or make hello.
  • If there are any errors in your code, it will alert you through the terminal and the compilation process will stop, in this section it is necessary to correct the errors by going to the text editor.
  • When the compilation is finished without problems, if you have compiled with the "make" command, you can run the compiled program by typing ./merhaba directly in the same directory. If you compiled with the "gcc" command, you can run the compiled program by typing ./a.out.
  • As an output, you will see the words "Hello World!" on the terminal screen.
c program structure
Save code in the Sublime Text editor.
c program structure
Compilation process with "make hello" command
c program structure
Running code compiled with ./hello

In this way, we learn that it is compiled in a simple way. In our next article, we will look at syntax in C.