Every C program consists of two major sections:
- Header Section
- 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
stdio.h
- This file includes pre-written functions for input and output operations, like
printf
andscanf
. - Example Usage:
#include <stdio.h>
math.h
- This header file provides mathematical functions, like calculating powers with
pow
and square roots withsqrt
.
$ cat math.h
_CRTIMP double --cdecl pow (double, double);
_CRTIMP double --cdecl pow (double);
Example Usage:
#include <math.h>
- 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, andmath.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()
:
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:
#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 useprintf
.- The
main()
function contains a singleprintf
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!
thanks xapet it’s amazing artical in c programme …