Basic Structure of C Program

 Structure of C Program:

// single line Comments

# pre-processor directives

prototype declarations

global variables

main() function

{

local variable declaration

statements

------      --------   -------

}

user_defined_function1()

{

variable declaration

statements

-----        -----        -----

}


Explanation:

1. Comments:

Comments can be places anywhere in the program; generally used for documentation purposes or to increase the readability of the code. There are two types of comments

1. Single line comments        2. Multi line comments

Single line comments starts with // followed by statement
ex: // this is a single line comment

Multi line comment start with /* and ends with */.
ex: /* This is
        multi line
        comment */

2. Preprocessors Directives:

  1. The pre-processors don’t execute the comment part while compiling the code.
  2. Pre-processor directives are processed through pre-processor before the C code is passed through compiler.
  3. The most commonly pre-processor directives are #include and #define (we will learn these soon)
  4. The #include is used to include header files and #define is used to define symbolic constants and macros.

3. Global variables:

There are some variables that can be used in whole program where ever necessary, these type of variables are to be declared as global variables.

4. main():

  1. Every C program has one or more functions present if one function is there it must be main() function only.
  2. Program execution starts from main() only; if there Is only one function in c program that too is not main() then it will compile successfully(if there are no errors) but no output is generated.
  3. The main() has two parts the local variable declaration and the statements part. Local variable scope/visibility is up to local function only
  4. Statements in main() executes line by line.

5. User Defined Functions:

  1. Sometimes user want to perform specific tasks according to requirement, so they can define their own in this section.
  2. These functions can be defined before or after main(); if defined after main() it must be prototyped before main() in prototype declaration section.
  3. There are two sections in user defined functions same as main(); variable declarations and statements. The variable used in functions are generally local variables.

Steps for execution of C program:

  1. Program Creation
  2. Program Compilation
  3. Program Execution

1. Program Creation:

    The program can be created using Editor. Different types of editors are GCC editor, Notepad, etc.

2. Program Compilation:

    The compilation is the process of converting code to machine understandable language.

3. Program Execution:

    After compilation, the executable file is created and the code is ready for execution.


Comments