2078 5

BIT102 ยท 2078 ยท 5 marks

What is the basic structure of a C program? Explain each part.

What is the basic structure of a C program? Explain each part. [5]

C ProgrammingChapter 1: Fundamentals of C ProgrammingBasic structure of C program

Quick answer

A C program is organized into several distinct sections. Below is the general template: Contains comments about the program such as program name, author, date, and purpose; Written using /; Ignored by the compiler; meant for human readability.

Basic Structure of a C Program


Basic Structure of a C Program

A C program is organized into several distinct sections. Below is the general template:

/* Documentation Section */
// Program description, author, date

/* Link Section (Header Files) */
#include <stdio.h>
#include <conio.h>

/* Definition Section (Macros/Constants) */
#define PI 3.14159

/* Global Declaration Section */
int globalVar;

/* Main Function */
int main()
{
    /* Local Declaration */
    int a, b;

    /* Executable Statements */
    printf("Hello, World!\n");

    return 0;
}

/* User-Defined Functions */
void myFunction()
{
    // function body
}

Explanation of Each Part

1. Documentation Section

  • Contains comments about the program such as program name, author, date, and purpose.
  • Written using /* ... */ (multi-line) or // (single-line).
  • Ignored by the compiler; meant for human readability.
  • Contains preprocessor directives that include standard library header files.
  • Syntax: #include <filename.h>
  • Example: #include <stdio.h> includes standard input/output functions like printf() and scanf().
  • These are processed before compilation.

3. Definition Section

  • Used to define symbolic constants or macros using #define.
  • Example: #define PI 3.14159
  • The preprocessor replaces every occurrence of PI with 3.14159 before compilation.

4. Global Declaration Section

  • Variables declared here are global variables, accessible throughout the entire program.
  • Declared outside all functions.
  • They retain their value for the entire lifetime of the program.

5. Main Function main()

  • Every C program must have exactly one main() function -- it is the entry point of execution.
  • Contains two parts:
    • Local Declaration: Variables declared inside main(), accessible only within it.
    • Executable Statements: The actual logic -- assignments, function calls, loops, etc.
  • Returns an integer value to the operating system (return 0 indicates successful execution).

6. User-Defined Functions

  • Functions written by the programmer to perform specific tasks.
  • Defined after (or before) main().
  • Promotes modularity and code reusability.

Summary Table

SectionPurpose
DocumentationComments and program description
Link SectionIncludes header/library files
Definition SectionDefines constants and macros
Global DeclarationDeclares global variables
main() FunctionEntry point; contains program logic
User-Defined FunctionsCustom functions for specific tasks

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.