DEV Community

Lawrence Juma
Lawrence Juma

Posted on

The Structure of the C Program

Overview

Most programming languages tend to have a structure or design, including C Language. C language is divided into five different sections which are: Documentation, Preprocessor command, Main() function, printf(…), and Return 0.

Introduction to the C program structure.

Every human being has a particular structure that includes a torso, head, neck, and four limbs. The vast majority of objects have a distinct structure. Every programming language has a unique structure in a manner akin to this. You would have to use these structures whenever writing the code.
The structure of the c program can be divided into different parts, each having its purpose. This makes the program easy to read, modify, and document and makes it consistent in format.

Basic structure.

  1. The documentation, which is typically given in the form of comments, includes a brief description of the program, the name of the programmer, the creation date, and more.
  2. Preprocessor command: This processor command instructs a C compiler in the stdio.h file to proceed with the actual compilation before the preprocessor command is executed.
  3. The main function, Main(), is where the program actually starts to run.
  4. Another function in C called printf(…) causes the text "Hello, World!" to be printed or displayed.
  5. Returning 0 closes/terminates the main function and gives the number 0.

Taking this example of c program. hello.c

/**                         // Documentation
  * file: hello.c
  * Author: your-name
  * description: program to print hello world
*/

#include <stdio.h>         // Proprocessor command

int main(){                // main function
printf("Hello World! \n);
return 0;

}
Enter fullscreen mode Exit fullscreen mode

Assemble and run a C program.

  1. By following these few instructions, this task should be simple.
  2. Add the required code in a file editor.
  3. Saving as hello.c
  4. Enter the command prompt and navigate to the saved file's directory.
  5. To compile your code, execute the command gcc hello.c.
  6. If there are no mistakes, the code will move on to the following line and create an executable program.
  7. In order to run your application, type a.out.
  8. The message "Hello World!" will appear on screen.
gcc hello.c
./a.out
Hello, world!
Enter fullscreen mode Exit fullscreen mode

Conclusion

  • The documentation, preprocessor command, main() function, printf(…), and return 0 sections are the six five sections in all.
  • The main() function is a requirement for all C programs, although the others are optional.
  • Well-structured C programs make debugging easier and increase readability and scalability.

Top comments (1)

Collapse
 
pauljlucas profile image
Paul J. Lucas

Preprocessor (with an 'e').