Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Structure of C Program with Example

Last Updated on July 13, 2023 by Mayank Dham

A structured approach is used in a C program, which includes preprocessor directives, function declarations, global variable declarations, and the main function. Preprocessor directives deal with header files and macros, whereas function declarations define the functions that will be used in the program. The main function serves as the entry point, and global variable declarations define shared variables. Understanding this structure aids in the creation of well-organized and efficient C programs. In this article, we will go over each component with examples to help you write well-structured code.

Basic Structure of C Program

Section Description
Documentation Includes the program’s description, the programmer’s name, and the creation date. The majority of these are written as comments.
Link Includes the program’s description, the programmer’s name, and the creation date. The majority of these are written as comments.
Definition preprocessor directive that contains symbolic constants is included. For instance, #define enables the usage of constants in our code. In the code, it substitutes its value for each constant.
Global Declaration Includes the declaration of functions, static global variables, and global variable declarations.
Main() Function Every C program’s main() function serves as the starting point for execution. A main() function must be present in every C program.
Subprograms All user-defined methods should be included (functions the user provides). Both built-in functions and function definitions specified in the Global Declaration section may be included there. The main() method is what is used for them.

In order to comprehend the structure of a C program, let’s look at an example:

Example: Write a program to calculate our age.

In the following example, we’ll calculate age concerning a year.

Let’s implement this and check:


/** //Documentation
 * file: age.c
 * author: you
 * description: a program to find our age.

#include <stdio.h>      //Link
#define BORN 2000       //Definition
int age(int current);   //Global Declaration
int main(void)          //Main() Function
  int current = 2021;
  printf("Age: %d", age(current));
  return 0;
int age(int current) {     //Subprograms
    return current - BORN;


You’ve to subtract the current year from your birth year and get your age.

Syntax of Structure in C Program

A structure’s syntax in C is defined by defining a structure name, followed by member variables with their respective data types. Structures enable the grouping of related data under a single name, resulting in a more organized approach to data management. The dot operator can be used to declare and access structure type variables. Utilizing structures in C helps improve code clarity and facilitates efficient handling of data.


In a C program, single-line comments can be written using two forward slashes i.e., //, and we can create multi-line comments using / /. Here, we’ve used multi-line comments.

 * file: age.c
 * author: you
 * description: a program to find our age.


This section includes all header files.
A header file is a file that contains C declarations that can be shared among many files. We can use other people’s codes in our files due to it. Before compilation, a copy of these header files is inserted into your code.



Any statement in C that starts with the symbol "#" is referred to be a preprocessor directive. Constants are made using the #define preprocessor compiler command. The usage of constants in our code is fundamentally made possible by #define, which enables the macro declaration.

#define BORN 2000

We’ve established the constant BORN, and we’ve given it the value of 2000. In general, it is better to define the constants using capital characters. Every time the aforementioned constant BORN is used in our code, 2000 will be utilized in its place.
In order to make a source program simple to edit and compile in many execution contexts, #define is frequently employed.
There is no semicolon at the conclusion of the defined sentence.

Global Declaration

All global variables, function declarations, and static variables are contained in this section. Anywhere in the program can utilize the variables specified in this section. They get access to all of the program’s features. Consequently, they are known as global variables.

int age(int current);

Our age function, which accepts a single integer parameter and outputs an integer, has been defined.

Main() Function

This section of a C program’s structure comprises the code’s primary purpose. The main() function is when the compiler begins running code. It may make use of user-defined functions as well as built-in and global variables. The main() function’s return type is not required to be an int and can alternatively be void.

int main(void)
  int current = 2021;
  printf("Age: %d", age(current));
  return 0;

Here, the number 2021 has been assigned to the variable current, which has been defined. After that, we invoked the age() method, which only accepts one parameter, before using the printf() function.


This also applies to the user-defined programs that the main() function calls. Regardless of their sequence, user-defined functions are typically written after the main() function.
The program’s control switches to the user-defined function when it is called from the main() function, and it returns to the main() function when it meets a return statement. In this instance, we’ve developed the age() function, which only accepts the current year as an input.

int age(int current) {
    return current - BORN;

The main function calls this function. The main function receives an integer as a response.

The structure of a C program adheres to a strict format to ensure proper organization and execution. Preprocessor directives, function prototypes, the main function, user-defined functions, and variable declarations are all common components of a C program. Following a well-defined structure improves the program’s readability, maintainability, and efficiency. It is critical to follow the structure when writing C programs in order to ensure proper compilation and execution.

Frequently Asked Questions (FAQs):

Q1: What is the basic structure of a C program?
A basic structure of a C program typically consists of the following elements:
Preprocessor directives
Function prototypes
The main function
User-defined functions

Q2: What are preprocessor directives?
Preprocessor directives are lines of code that start with a ‘#’ symbol. They are used to include header files, define macros, or perform conditional compilation. Examples of preprocessor directives include ‘#include’, ‘#define’, and ‘#ifdef’.

Q3: What is the main function in a C program?
The main function is the entry point of a C program. It is where the program execution begins. Every C program must have a main function, which returns an integer value to indicate the program’s exit status.

Q4: What are function prototypes?
Function prototypes are declarations that specify the name, return type, and parameters of a function before its actual implementation. They provide a forward declaration to the compiler, allowing it to recognize and validate the function calls made within the program.

Q5: Can you provide an example of a C program structure?
Certainly! Here’s an example of a simple C program structure:

// Function prototype
void greet();

int main() {
    // Main function body
    printf("Starting the program.\n");
    printf("Program execution complete.\n");
    return 0;
// User-defined function
void greet() {
    printf("Hello, world!\n");

In this example, the program includes the stdio.h header file using the preprocessor directive #include. It declares a function prototype for the greet function, followed by the main function that prints messages and calls the greet function. Lastly, the greet function is defined to print a greeting message.

Leave a Reply

Your email address will not be published. Required fields are marked *