C Program Basics

C is a structured programming language. Every c program and its statements must be in a particular structure. Every c program has the following general structure...

general structure of c,c program general structure,c general structure

Line 1: Comments - They are ignored by the compiler

This section is used to provide small description of the program. The comment lines are simply ignored by the compiler, that means they are not executed. In C, there are two types of comments.

  1. Single Line Comments: Single line comment begins with // symbol. We can write any number of single line comments.
  2. Multiple Lines Comments: Multiple lines comment begins with /* symbol and ends with */. We can write any number of multiple lines comments in a program.

In a C proogram, the comment lines are optional. Based on the requirment, we write the comments. All the comment lines in a C program just provide the guidelines to understand the program and its code.

Line 2: Preprocessing Commands

Preprocessing commands are used to include header files and to define constants. We use #include statement to include header file into our program. We use #define statement to define a constant. The preprocessing statements are used according to the requirment. If we don't need any header file,then no need to write #include statement. If we don't need any constant, then no need to write #define statement.

Line 3: Global Declaration

Global declaration is used to define the global variables, which are common for all the functions after its declaration. We also use the global declaration to declare functions. This global declaration is used based on the requirment.

Line 4: int main()

Every C program must write this statement. This statement (main) specifies the starting point of the C program execution. Here, main is a user defined method which tells the compiler that this is the starting point of the program execution. Here, int is a datatype of a value that is going to return to the Operating System after completing the main method execution. If we don't want to return any value, we can use it as void.

Line 5: Open Brase ( { )

The open brase indicates the begining of the block which belongs to the main method. In C program, every block begins with '{' symbol.

Line 6: Local Declaration

In this section, we declare the variables and functions that are local to the function or block in which they are declared. The variables which are declared in this section are valid only within the function or block in which they are declared.

Line 7: Executable statements

In this section, we write the statements which perform tasks like reading data, displaying result, calculations etc., All the statements in this section are written according to the requirment.

Line 9: Closing Brase ( } )

The close brase indicates the end of the block which belongs to the main method. In C program every block ends with '}' symbol.

Line 10, 11, 12, ...: Userdefined function()

This is the place where we implement the userdefined functions. The userdefined function implementation can also be performed before the main method. In this case, the user defined function need not to be declared. Directly it can be implemented, but it must be before the main method. In a program, we can define as many userdefined functions as we want. Every user defined function needs a function call to execute its statements.

General rules for any C program

  1. Every executable statement must end with semicolon symbol (;).
  2. Every C program must contain exactly one main method (Starting point of the program execution).
  3. All the system defined words (keywords) must be used in lowercase letters.
  4. Keywords can not be used as user defined names(identifiers).
  5. For every open brase ({), there must be respective closing brase (}).
  6. Every variable must be declared bofore it is used.