Saturday 7 November 2015

BASIC STRUCTURE OF C PROGRAM 1

  • The C program usually contain one or more sections.
  • To write a C program we first create a function main to accomplish a specific task
  • A function is a subroutine that contains the statements, enclosing them within { and } to perform the required task.
  • Except the function main(), rest of the sections are optional.
  • Different sections of a C-Program are shown below :

STRUCTURE OF C-PROGRAM





DOCUMENTATION SECTION

  • Contains comments such as : program description, programmer's name, date on which it is written and other details required by the program
  • A comment in a C program is ignored by the compiler
  • '/*' is used to mark the start of a comment and it terminate with '*/'
  • There must be no gap between the asterisk (*) and slash (/)
  • Comments are used to enhance program readability and understandability by giving the description or other detail about the program
  • Comments may be placed anywhere in the program, but are not allowed in the middle of any keyword or identifier

LINK SECTION


  • It helps to include the external file functions from the system library.
  • We must add an #include instruction in the program, which instructs the compiler to link the specified function in the library.
  • These library files are called as header files.
  • Header files can optionally be included in the program as per the requirement.
  • When we compile the C program,the header files are also compiled with the original program.
  • All the header files are included into the program by preprocessor directives.
  • Preprocessor directives are processed before a source program is handed over to the compiler.
  • these should be placed in a source program before the function main()
  • All the preprocessor directives should start with # and end without semicolon(;)

e.g:

    • All the standard mathematical functions (e.g : sin, cos, exp, etc.) are defined and kept as a part of C math library <math.h>
    • Some standard input/ output functions are defined in standard input/output header file, <stdio.h>




DEFINITION SECTION

  • This section defines all the symbolic constants.
  • Symbolic constants are defined a value using the #define instruction.
  • Whenever a symbolic name is encountered, the compiler substitutes the value associated with the name automatically
  • The values remain constant throughout the program, and are not allowed to re define the values in the program
  • #define is a pre-processor directive
  • #define lines should not end with a semicolon (;)
  • Generally symbolic constants are written in uppercase

GLOBAL DECLARATION SECTION


  • This section contains variables that are used more than one one function. 
  • Any variable which is declared in this section is called as global variable and are available to all the functions


No comments:

Post a Comment