Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

main: the program entry point

The main function is the starting point of a C program in a hosted environment. Its result reports program termination to that environment.

Version: C23

Syntax

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

The simplest portable form is int main(void). int specifies the return type, and void inside the parentheses specifies no parameters. Braces enclose the function body. Returning 0 reports successful termination. <stdlib.h> provides the named status values EXIT_SUCCESS and EXIT_FAILURE.

For command-line arguments, use int main(int argc, char *argv[]). argc gives the argument count; argv points to the first element of an array of pointers to strings. Check argc > 1 before reading argv[1]. A program started without additional arguments has no first user-supplied argument.

Reaching the end of the initial invocation of main returns 0 in C99 and later. An explicit return 0; still makes this step clear in an example. void main() is not a portable replacement for the standard form.

Do not confuse a return value with displayed text: return 5; does not print 5. Freestanding environments, including some microcontroller programs, have implementation-defined startup rules; this entry describes an ordinary hosted program.

Examples

#include <stdio.h>

int main(void) {
    puts("CKTIS");
    return 0;
}

Result

CKTIS

Sources