Structure and Syntax of C
A C program can have some particular sections like header, function, variable, etc. These sections are called part of the program. The parts of the program are studied under the Structure of C Program.
C has some following sections in a program:
- Documentation Section
- Preprocessor Section (Link Section)
- Global Declaration Section
- Function Prototype Section
- Main Function Section
- Definition Section (Function Definitions)
Let's take a look at an example to understand the structure of C programming language.
C
// By Darpan Kattel
// Global declaration section
#include <stdio.h>
// Function prototype section
int main()
{
printf("Hello World!\n");
return 0;
}
Explanation
// By Darpan Kattel As a result, those comments act like documentation, including the name of the program or the author of the program. That is the documentation section. Line 4 contains the preprocessor directive, which is used to include or link the header file, here stdio.h. File stdio.h contains the definitions of the functions like printf, scanf, etc. which are required to print the output. This is the preprocessor section. #include <stdio.h> Line 6 contains the function main, which is the entry point of the program. It is the main function of the program. Generally, every program statements are written inside the main function. int main() The line 9 contains the return statement, which is used to return the value 0 to the operating system, or you can understand it as the program is finished. return 0;
Global Declaration Section and Function Prototype Section
#include <stdio.h>
// Function prototype section
int main() The function prototype section is the place where all the functions are declared. It becomes easier to understand the program if we declare all the functions before the main function, as the compiler takes the top to bottom approach to understand the program.
Examples of Global Declaration Section and Function Prototype Section
int myGlobalVariable = 0; // Global declaration
int myFunction (int myParameter); // Function prototype
Syntax
In the above examples, you must have seen that the syntax is similar to the English language, although some brackets, semi-colons, and other symbols are used.
The semi-colon ; is used to separate the statements, i.e; it is a statement terminator.
The curly braces {} are used to group the statements. It is called a block.
The parenthesis () are used to group the expressions. It is called an expression group.