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.
- 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.
- 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.
- The main function, Main(), is where the program actually starts to run.
- Another function in C called printf(…) causes the text "Hello, World!" to be printed or displayed.
- 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;
}
Assemble and run a C program.
- By following these few instructions, this task should be simple.
- Add the required code in a file editor.
- Saving as hello.c
- Enter the command prompt and navigate to the saved file's directory.
- To compile your code, execute the command gcc hello.c.
- If there are no mistakes, the code will move on to the following line and create an executable program.
- In order to run your application, type a.out.
- The message "Hello World!" will appear on screen.
gcc hello.c
./a.out
Hello, world!
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)
Preprocessor (with an 'e').