C Programs Tutorials | IT Developer
IT Developer

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:

  1. With int return type:

C

#include <stdio.h>

int main() {

    // Code to be executed

    return 0;

}

  1. 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

  1. 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.
  2. 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.