Structure of C Program

Sunny Bhaskar

10/23/20241 min read

A C program is written in a specific structure, following certain rules and components to make it understandable and executable by the compiler. Here’s a breakdown of the basic structure of a C program:

1. Preprocessor Directives

These are the lines that begin with a `#`. They provide instructions to the compiler before the actual compilation starts.

The most common directive is `#include`, which is used to include header files.

Example

#include <stdio.h> // Includes the Standard Input Output header file

2. Global Declarations

Variables and function prototypes can be declared globally, meaning they can be used throughout the entire program.

Example

int globalVar; // Global variable

3. Main Function

Every C program must have a `main()` function. It’s the starting point of execution.

The main function can return an integer value (`int`), indicating success or failure of the program execution.

Example

int main() {

// Code inside main function

return 0; // Indicates that the program ended successfully

}

4. Variable Declaration

Inside the `main()` function, you can declare local variables that are used within the function.

Example

int a, b; // Declaring variables

5. Executable Statements

These are the actual C statements that perform operations, such as calculations, input/output, and function calls.

Example

a = 5;

b = 10;

int sum = a + b; // Executable statement

6. Input/Output Statements

Functions like `printf()` and `scanf()` are used for output and input operations.

Example

printf("The sum is: %d", sum); // Output

7. Return Statement

The `main()` function generally ends with a return statement, which returns a value to the operating system.

Returning `0` typically indicates that the program finished successfully.

Example

return 0;

Address

Prayagraj Phaphamau

Contacts

7518998334
sunnygamingyt298@gmail.com

Subscribe to our newsletter