C Program Basics
C is a structured programming language. Every c program and its statements must be declared in a particular structure. Every c program has the following 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.
- Single Line Comments: Single line comment starts with // symbol. We can write any number of single line comments.
- Multiple Lines Comments: Multiple lines comment starts with /* symbol and ends with */. We can write several number of multiple lines comments in a program.
In a C programming language, the comment lines are optional, that is based on the requirement, we write the comments. In a C program all the comment lines just provide the guidelines to understand the programs and their code.
Line 2: Preprocessing Commands
Preprocessing commands are used to include header files and to define constants. We use #include to include header file into our program. We use #define to define a constant. The preprocessing statements are used according to our needs. If we don’t need any header file,then there is no need to write #include statement. If we don’t need any constant, then there is 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 their declaration. We also use the global declaration to declare functions. This global declaration is used based on our needs also.
Line 4: int main()
Every C program must write this statement. Without this statement we can not execute our program. 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(OS) 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 starting of the block which belongs to the main method. In C program, every block begins with open brase ‘{‘ symbol.
Line 6: Local Declaration
In this section, we declare the variables and functions that are local to the function 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 works like reading data, showing result, calculations etc., All the statements in this section are written according to our requirements.
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 closing brase ‘}’ symbol.
Line 10, 11, …: 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.
Some general rules for any C program are
- Every executable statement must ends with semicolon symbol (;).
- Every C program must contain exactly one main method (Begining point of the program execution).
- All the keywords must be used in lowercase letters.
- Keywords can not be used as user defined names(identifiers).
- For every open brase ({), there must be respective particular closing brase (}).
- Every variable must be declared bofore it is being used.