Search Result
Details here...

Structure and Syntax of C

Previous

Introduction to C

Next

Comments in 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:


  1. Documentation Section
  2. Preprocessor Section (Link Section)
  3. Global Declaration Section
  4. Function Prototype Section
  5. Main Function Section
  6. Definition Section (Function Definitions)

Let's take a look at an example to understand the structure of C programming language.


C

// A simple Hello World Program
// By Darpan Kattel
// Global declaration section
#include <stdio.h>
// Function prototype section
int main()
{
printf("Hello World!\n");
return 0;
}

Explanation

In the above code, the line 1 and 2 contain comments, which are ignored by the compiler. // A simple Hello World Program
// 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.

Don't know what's a comment?

Don't worry, we will learn more about comments in other chapters, later.

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

The global declaration section is the place where all the global variables, that is, variables that are declared outside of any function, are declared. They can be used anywhere in the program. // Global declaration 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

Didn't get something?

Don't worry, we will learn everything in detail in upcoming chapters.

Syntax

Syntax is the grammar of the programming language. It is the rule of the language.

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.
By darpan.kattel
Previous

Introduction to C

Next

Comments in C