
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Call the Main Function in C Program
In this article, we'll discuss how to call the main() function in a C program. The main() function is the starting point and runs automatically when the program begins. However, there may be situations where you need to call it from other parts of your code. We'll explain how this works.
We'll also cover recursion in the main() function and different ways to call main() within a C program.
What is the Main Function?
In C, the main() function is a special function that acts as the starting point of the program. Every C program must have one main function, and when you run the program, execution begins from this function.
The main function is usually written like this:
int main() { // Your code goes here return 0; }
The int before main() indicates that the function returns an integer value. This value is used to show whether the program ran successfully (return 0) or if there was an error (non-zero return).
Can we call the main function?
In C, the main function is automatically called by the operating system when the program runs, and it behaves like any other function, having a return type. Although you can call the main() function within itself, this is called recursion. Recursion simply means a function calling itself. However, it is generally not recommended to call the main() function this way, as such use cases are rare.
Understanding Recursion in the main() Function
In recursive programming, a function calls itself to solve a problem. The main() function can also call itself, which is known as recursion.
Recursion relies on the function call stack, which temporarily stores function calls during execution. When main() calls itself, each new call is pushed onto the stack. If there isn't a proper exit condition, the stack will continue to grow, eventually causing the system to run out of memory, leading to a stack overflow.
Example
In this example, we use recursion with a counter (count) to limit how many times the main() function is called. The recursion stops when count reaches 3, preventing infinite calls.
#include <stdio.h> int main() { static int count = 0; if (count < 3) { // Base condition to stop recursion printf("Calling main() recursively
"); count++; main(); // Calling main() within itself } return 0; }
The output will show "Calling main() recursively" three times, stopping when the count reaches 3.
Calling main() recursively Calling main() recursively Calling main() recursively
Calling main() From Another Program
Apart from recursion, we can also call the main() function from another program. This is done by including a header file with the prototype of the main() function, allowing us to call it from another C file.
Here's how we can call the main() function from one C program to another:
- first.c: Contains the main() function.
#include <stdio.h> int main() { printf("Main function from first.c
"); return 0; }
#include "first.h" // Include the header from first.c int main() { printf("Calling main from first.c in another.c
"); return main(); // Call main() from first.c }
#ifndef FIRST_H #define FIRST_H int main(); // Function prototype #endif
By including the header file in another.c, we enable it to call the main() function from first.c. The compiler automatically connects the two files during the linking process.
Using a Function Pointer to Call main()
Another advanced technique to call main() is by using function pointers. A function pointer is a pointer that points to a function, and you can use it to call main() dynamically. This approach adds flexibility by allowing you to call main() indirectly.
Example
In this example, we declare a function pointer main_ptr that points to the main function. Inside the call_main() function, we use main_ptr() to call the main() function. When call_main() is called, it triggers the main() function, which then executes and prints "Hello, World!" to the console
#include <stdio.h> int main() { printf("Hello, World!
"); return 0; } int call_main() { // Calling the main function using a function pointer int (*main_ptr)() = main; return main_ptr(); }
The output will display "Hello, World!" when the main() function is called through the function pointer main_ptr().
Hello, World!
Risks of Calling main()
While it's possible to call main() using recursion, function pointers, or from another program, there are some risks involved:
- Stack Overflow: Each recursive call adds a new frame to the stack. Without proper checks, it can exceed the stack limit and cause a stack overflow.
- Infinite Recursion: If there's no base condition in the recursion, the program could keep calling main() indefinitely, causing the program to crash.
Conclusion
In this article, we showed that calling main() through recursion, function pointers, or from another program is possible but should be done carefully. Always make sure recursion has a base case, and use function pointers with a clear understanding of how they work.