Every C program consists of two major sections:
  1. Header Section
  2. Main Section

These two sections together form a complete C program. Let’s explore each in detail:


Section 1: Header Section

In the header section, we include standard header files that contain pre-written code for various functions. These files, located in the /usr/include directory, allow us to use essential functions without rewriting them from scratch.

Example header files:
 . stdio.h
 . math.h

Why Do We Need This Header Section?

The header section includes standard header files, such as stdio.h, that give access to functions like printf and scanf for input and output, along with other files for additional functionalities.

Common Header Files

  1. stdio.h
  • This file includes pre-written functions for input and output operations, like printf and scanf.
  • Example Usage:
    #include <stdio.h>
  1. math.h
  • This header file provides mathematical functions, like calculating powers with pow and square roots with sqrt.
   $ cat math.h
   _CRTIMP double --cdecl pow (double, double);
   _CRTIMP double --cdecl pow (double);

Example Usage:

   #include <math.h>
  1. Other Header Files in /usr/include Directory
  • Here are some examples of header files you may find in this directory:
xapet@xapet:/usr/include$ ls
  • Here are some other header files commonly found in this directory: X11, complex.h, elf.h, fenv.h, linux, mqueue.h, pthread.h, string.h, threads.h, values.h, aio.h, cpio.h, endian.h, grp.h, llvm-11, math.h, net, etc.
  • These files provide various utilities and specialized functions.
   xapet@xapet:/usr/include$ cat stdio.h

Note: To use specific functions, you must include the relevant header file. For instance, stdio.h is needed for input/output functions, and math.h for math functions.

// **Remember to `include` the header file, such as `stdio.h`, to use its functions.**
  Point: This is the header section where we include necessary header files.

Section 2: Main Section

The main section is essential for every C program. It contains the main() function, which serves as the entry point for the program’s execution.

The main() Function

When you run a C program, the operating system starts executing code from the main() function. The code you write inside this function defines the program’s actions.

  • Basic Structure of main():
C
  int main() {
      // Code goes here
      return 0; // Indicates successful program execution
  }

Example Program

Here’s an example of a simple C program with both header and main sections:

C
#include <stdio.h>  // Header Section

int main() {        // Main Section
    printf("Hello, World!\n");  // Code inside main function
    return 0;
}

In this example:

  • #include <stdio.h> includes the standard input-output library, allowing the program to use printf.
  • The main() function contains a single printf statement that outputs “Hello, World!” to the screen.

Additional Notes

  • Header Files: Include only the header files you need for your program, keeping the code efficient and well-structured.
  • Structure Order: The header section usually appears first, followed by the main section, as is standard in C programs.

Info: Both the header and main sections together make up a complete C program.

With this basic structure, you’re ready to start creating and running simple C programs!

Categorized in: