✏️ Explanatory Question

Execution of a C program starts from which function?

👁 1,286 Views
📘 Detailed Answer
🟢 Easy
💡

Answer with Explanation

C Programming Language

Execution of a C Program Starts from Which Function?

Every C program begins its execution from the main() function. It serves as the entry point of the program, and the operating system transfers control to this function when the program starts.

Answer: The execution of a C program always starts from the main() function, regardless of where it appears in the source code. It is known as the entry point of every C program.

Why Does Execution Start from main()?

When a C program is executed, the operating system looks for the main() function. Once it finds this function, program execution begins from its first statement and continues sequentially until the function ends or a return statement is encountered.

PROGRAM EXECUTION FLOW
Program Startsmain()Other Functions (if called)Program Ends

Basic Example


#include 

int main()
{
    printf("Program execution starts here.");

    return 0;
}

Output


Program execution starts here.

Example with Multiple Functions

Even if a program contains several functions, execution still begins with main(). Other functions execute only when they are called from main() or another function.


#include 

void display()
{
    printf("This is the display() function.\n");
}

int main()
{
    printf("Execution starts from main().\n");

    display();

    printf("Back to main().");

    return 0;
}

Output


Execution starts from main().
This is the display() function.
Back to main().

Execution Flow Diagram


Program Starts
       │
       ▼
   main()
       │
       ▼
display()
       │
       ▼
Return to main()
       │
       ▼
 Program Ends

Characteristics of the main() Function

Feature Description
Entry Point Execution of every C program starts from main().
Return Type Generally declared as int main().
Return Value Usually returns 0 to indicate successful execution.
Can Call Other Functions Yes, main() can call any user-defined or library function.
Mandatory Every executable C program must contain exactly one main() function.

What Happens If There Is No main() Function?

Without main()

  • The compiler or linker cannot find the program's entry point.
  • The executable cannot be created successfully.
  • The program cannot be executed.

With main()

  • Program execution begins correctly.
  • Other functions can be called as needed.
  • The program terminates normally after main() finishes.

Common Mistakes

Avoid These Mistakes

  • Assuming execution starts from the first function written in the file.
  • Writing multiple main() functions in one program.
  • Forgetting to return an integer value from main().
  • Calling main() recursively without a valid reason.

Best Practices

  • Always declare main() with the int return type.
  • Return 0 after successful execution.
  • Keep main() simple and delegate tasks to other functions.
  • Write modular and reusable code.

Prerequisites

Before Learning This Topic

  • Basic understanding of C program structure.
  • Knowledge of C functions.
  • Understanding of the printf() function.
  • Basic familiarity with the program compilation and execution process.

Interview Questions

  1. Which function acts as the entry point of a C program?
  2. Can a C program execute without a main() function?
  3. Why is main() declared with the int return type?
  4. Can a program contain more than one main() function?
  5. Does execution start from the first function written in the source file?

Key Takeaway

The execution of every C program begins with the main() function. It is the program's entry point, and the operating system transfers control to it when the program starts. All other functions are executed only when they are called directly or indirectly from main().