A C program follows a standard structure that makes it organized, readable, and easy to maintain.

1. Documentation Section

  • Contains comments describing the program.
  • Explains the purpose, author, and date.

Example:

/* Program to print Hello, World! */

2. Link Section

  • Includes the required header files using the #include directive.

Example:

#include <stdio.h>

3. Definition Section

  • Defines symbolic constants using the #define directive.

Example:

#define PI 3.14159

4. Global Declaration Section

  • Declares global variables and function prototypes.
  • These can be accessed throughout the program.

Example:

int total;
void display();

5. Main() Function

  • The execution of every C program starts from the main() function.
  • It contains declarations, executable statements, and a return statement.

Example:

int main()
{
    printf("Hello, World!\n");
    return 0;
}

6. User-Defined Functions

  • Functions written by the programmer to perform specific tasks.
  • These are called from the main() function.

Example:

void display()
{
    printf("Welcome to C Programming");
}

Complete Example

/* Program to Demonstrate Structure of a C Program */

#include <stdio.h>

#define PI 3.14159

void display();

int main()
{
    printf("This is the main function.\n");
    display();
    return 0;
}

void display()
{
    printf("Welcome to C Programming.\n");
}

Output

This is the main function.
Welcome to C Programming.

Summary

SectionPurpose
DocumentationDescribes the program using comments
LinkIncludes header files
DefinitionDefines constants and macros
Global DeclarationDeclares global variables and function prototypes
main() FunctionStarting point of program execution
User-Defined FunctionsPerforms specific tasks to improve modularity

Related Posts