C Programming Tutorial 0/65 lessons ~6 min read Lesson 19

    Main Function

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

    Course progress0%
    Focus
    10 guided sections
    Practice signal
    Examples included
    Career prep
    Foundation builder

    Introduction

    The main function is the entry point of every C program. OS calls main when the executable starts.

    Understanding the topic

    Signatures int main(void) or int main(int argc, char *argv[])

    Return value return 0 for success; non-zero for error codes.

    argc/argv Argument count and string array of command-line args.

    • Signatures — int main(void) or int main(int argc, char *argv[]).
    • Return value — return 0 for success; non-zero for error codes.
    • argc/argv — Argument count and string array of command-line args.

    Step-by-step explanation

    1. Signatures — int main(void) or int main(int argc, char *argv[]).
    2. Return value — return 0 for success; non-zero for error codes.
    3. argc/argv — Argument count and string array of command-line args.

    Syntax reference

    Syntax reference:

    c
    int main(void) { return 0; }

    Informative example

    Example program:

    c
    #include <stdio.h>
    int main(int argc, char *argv[]) {
    printf("argc=%d program=%s\n", argc, argv[0]);
    return 0;
    }

    Execution workflow

    1Main Function — step by step
    1 / 3

    Signatures

    int main(void) or int main(int argc, char *argv[]).

    Best practices

    • Enable warnings: gcc -Wall -Wextra -std=c11 source.c -o app
    • Give every variable a defined value before it is read.
    • Stay inside array bounds — C will not stop you from over-running a buffer.

    Common mistakes

    • Reading uninitialized storage — behavior is undefined.
    • Dismissing compiler warnings instead of fixing root causes.
    • Ignoring NULL returns from malloc, fopen, and similar APIs.

    Hands-on exercise

    Practice problems:

    • Print all command-line arguments
    • Return different exit codes

    Summary

    Main Function in C — Program entry — signatures, argc/argv, exit codes.

    Ready to mark this lesson complete?Track your journey across the entire course.