Book
Collection
Click for Table of Contents
© 2025 by Rance D. Necaise
C Primer for Python Programmers
Copyright © 2025
Rance D. Necaise

2.1 The Main Function

All programs must have a starting point for execution, which varies from language to language. In Python, the first statement executed is the first statement encountered in the driver module outside of all functions and classes. Although not required in Python, it is good programming practice to place all statements within functions and to specify one function as the starting point. Traditionally, that function is called main, which must be called explicitly at the end of the driver program.

In C, all statements must be included within functions and every program must have a function named main. The main function will be called automatically when the program is executed. Thus, the first statement executed is the first statement within the main function. Consider the "Hello World" program from the previous chapter:

  1. /* hello.cc
  2.    The hello world program in C.
  3.  */
  4.  
  5. #include <stdio.h>
  6.  
  7. int main()
  8. {
  9.   printf("Hello World!\n");
  10. }

When the program is compiled and executed, the first statement in the main function will be called to begin the program. In this case, it is a call to the function printf.