- 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 - C Main Function
![]() Share with a Friend |
C Programming - C Main Function
C main Function
The main function is the entry point of every C program. It is the function that is automatically executed when a C program starts. The main function is mandatory in every C program, and without it, the program will not run.
Syntax of the main Function
The main function can be written in two commonly used forms:
- With int return type:
C
#include <stdio.h>
int main() {
// Code to be executed
return 0;
}
- With void return type:
C
#include <stdio.h>
void main() {
// Code to be executed
}
In modern C standards (like C99 and later), it is recommended to use the int main() version as it allows returning an exit status to the operating system.
Explanation of the main Function
- Return Type:
- int main(): In most cases, the main function returns an integer value, typically 0 to indicate successful execution of the program. Non-zero values can be returned to signal errors.
- void main(): This is an older practice where no value is returned. However, it is not recommended in modern C standards.
- Parameters (optional):
- The main function can also take command-line arguments through its parameters, as in:
C
int main(int argc, char *argv[]) {
// Code to handle command-line arguments
}
- argc: The argument count, which tells how many command-line arguments are passed to the program.
- argv[]: An array of strings (character pointers) representing the actual arguments passed to the program.
Basic Example of the main Function
C
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
Explanation:
- The program prints Hello, World! to the screen and then returns 0 to indicate successful execution.
Output:
Hello, World!
Example with Command-line Arguments
C
#include <stdio.h>
int main(int argc, char *argv[]) {
if (argc < 2) {
printf("Please provide at least one argument.\n");
return 1;
}
printf("Arguments passed: \n");
for (int i = 0; i < argc; i++) {
printf("%s\n", argv[i]);
}
return 0;
}
Explanation:
- The main function accepts command-line arguments and prints them one by one.
- The argc variable stores the number of arguments, while argv[] stores the actual arguments passed.
Example Command Line Input:
bash
$ ./program Hello World
Output:
bash
Arguments passed:
./program
Hello
World
Return Value of main
- The return 0; statement in main indicates the successful termination of the program.
- A return value other than 0 (e.g., return 1;) can be used to indicate that the program encountered an error. This return value can be used by the operating system or by other programs that execute the C program to determine the program's success or failure.
Summary of the main Function
- The main function is the entry point of a C program.
- It can return an integer (int main()) or void (void main()), with int main() being more common and recommended.
- Command-line arguments can be passed to the main function via argc and argv[].
- The return value of main signals the success (0) or failure (non-zero) of the program to the operating system.
