Understanding the Structure of C Programs

Aug 8, 2024

Structure of a C Program

Introduction

  • Overview of the basic structure of a C program.
  • Discussion of sections in a C program.
  • Some sections are optional, while others are essential.

1. Documentation Section (Comment Section)

  • Purpose: Provides information about the program.
  • Includes:
    • Author's name (e.g., Jayanti Khatri)
    • Date and time of development
    • Brief description of the program (e.g., "Program for addition of two numbers")
  • Comments:
    • Single-line comments: // (e.g., // Program for addition of two numbers)
    • Multi-line comments: /* ... */
  • Importance:
    • Helps future users understand the code.

2. Link Section

  • Includes header files necessary for the program.
  • Example:
    • #include <stdio.h> (Standard Input Output Library)
  • Purpose of header files:
    • To provide definitions for built-in functions like printf and scanf.
  • Other common header files:
    • #include <conio.h> (for console input/output)
    • #include <math.h> (for math functions)
    • #include <string.h> (for string functions)
  • Importance:
    • The linker integrates these functions into the program.

3. Definition Section

  • Defines symbolic constants using #define.
  • Example:
    • #define PI 3.14
    • Use of macros to simplify code and make it easier to update.

4. Global Declaration Section

  • Declaration of global variables and user-defined functions.
  • Global variables can be accessed by all functions in the program.
  • Local variables are accessible only within the function they are declared in.
  • Example:
    • int a; (global declaration)

5. Main Section

  • Essential and must be included in every C program.
  • Only one main function is allowed per program.
  • Structure:
    • void main() {}
    • Two parts:
      • Declaration part (optional)
      • Executable part (must contain at least one statement)
  • Example:
    • `printf(