- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
C Programming - Structure of C Program
![]() Share with a Friend |
C Programming - Structure of a C Program
Introduction
A C program is composed of several components, each serving a specific purpose. The structure of a C program is divided into various sections, which are typically arranged in a specific order. Below is a detailed explanation of the components and their arrangement:
Preprocessor Directives
- Purpose: Preprocessor directives are used to include external files or define constants that are needed for the program.
- Syntax: Preprocessor directives begin with the # symbol.
Example:
#include <stdio.h> // Includes the standard input/output header file #define PI 3.14 // Defines a constant for Pi
- Common Preprocessor Directives:
- #include: Used to include header files.
- #define: Used to define constants or macros.
- #ifdef, #endif: Conditional inclusion of code.
Global Declarations
- Purpose: Global variables or functions that are accessible throughout the entire program can be declared here.
Example:
int globalVar = 10; // A global variable
Main Function
- Purpose: The execution of a C program starts from the main() function. It serves as the entry point for program execution.
- Syntax: The main() function is where the program logic begins. It may return an integer value (often used to indicate the status of the program).
Example:
#include <stdio.h> // Preprocessor directive int main() { // Program statements go here return 0; // Indicates that the program ended successfully }
- Types of Main Functions:
- int main(): Returns an integer value.
- void main(): Does not return any value (although this is not recommended by the C standard).
Local Declarations
- Purpose: Variables that are used inside functions or blocks are declared here. These variables are known as local variables and have a limited scope (only within the function or block).
Example:
int num1, num2; // Local variables in main()
Executable Statements
- Purpose: These are the statements where the main logic of the program is written. They execute the operations defined in the program.
Example:
#include <stdio.h> // Preprocessor directive int main() { printf("Enter two numbers: "); // Print statement scanf("%d %d", &num1, &num2"); // Taking input from user printf("The sum is: %d", num1 + num2); // Output the sum return 0; // Exit the program }
Functions
- Purpose: Functions are blocks of code that perform specific tasks. They can be called from the main() function or other functions.
- Syntax: A function has a name, a return type, and a body.
Example:
int add(int a, int b) { return a + b ; // Exit the program }
- Functions can be:
- User-defined functions: Functions created by the programmer.
- Standard functions: Functions that come with C libraries (e.g., printf(), scanf()).
Basic Structure Example:
Here’s an example C program that demonstrates the structure:
Example:
// Preprocessor Directives
#include <stdio.h> // Includes the standard input/output header// Global Declarations
int globalVar = 10; // A global variable// Function Declaration (Optional)
int add(int a, int b);// Main function
int main() {// Local Declarations
int num1, num2, sum; // Local variables in main()// Input Statements
printf("Enter two numbers: "); // Print statement scanf("%d %d", &num1, &num2"); // Taking input from user// Function Call (Calling the add function)
sum = add(num1, num2);
// Output Statement
printf("The sum is: %d", sum); // Output the sum return 0; // Indicating successful execution } // Exit the program// Function Definition
int add(int a, int b) { return a + b ; }
Explanation of Components in the Example:
- Preprocessor Directive:
- #include <stdio.h> is used to include the standard input/output library, which provides functions like printf() and scanf().
- Global Declarations:
- int globalVar = 10; is a global variable that can be accessed by all functions in the program.
- Main Function:
- int main() contains the main logic of the program, where two integers are input from the user, passed to the add() function, and the result is printed.
- Local Declarations:
- Inside main(), local variables (num1, num2, and sum) are declared to store the numbers and their sum.
- Function Definition:
- The add() function takes two arguments and returns their sum. This function is called from main().
Summary of the C Program Structure:
- Preprocessor Directives (optional)
- Global Declarations (optional)
- Main Function (int main() or void main())
- Local Declarations (within the main function or other functions)
- Executable Statements (main logic of the program)
- Functions (user-defined or standard library functions)
This structure is fundamental for writing well-organized and efficient C programs.
