0
0
Cprogramming~5 mins

main function and program entry

Choose your learning style9 modes available
Introduction

The main function is where a C program starts running. It tells the computer what to do first.

When you want to create a program that runs from start to finish.
When you need a place to begin your instructions for the computer.
When you want to organize your program so it has a clear starting point.
Syntax
C
int main() {
    // your code here
    return 0;
}

The int before main means the function returns a number.

return 0; tells the system the program finished successfully.

Examples
Simple main function that does nothing but ends successfully.
C
int main() {
    return 0;
}
Main function that can receive command line arguments.
C
int main(int argc, char *argv[]) {
    return 0;
}
Sample Program

This program starts at main, prints a greeting, then ends.

C
#include <stdio.h>

int main() {
    printf("Hello, world!\n");
    return 0;
}
OutputSuccess
Important Notes

Every C program must have exactly one main function.

The program always starts running from main.

Summary

The main function is the entry point of a C program.

It usually returns an integer to show if the program ended well.

Use return 0; to say the program finished without errors.