Skip to content
Home » All Posts » The Beginning of a Program – The C Main Function

The Beginning of a Program – The C Main Function

the c main function

What is the Main() Function in C?

A C application can be understood as a collection of functions interacting with each other to achieve certain functionalities. The main() function is the entry point of a C application. All C application must include a main() function and it must return an integer value (also known as the return code of an application). Typically, a return code of 0 indicates a successful execution while other values indicate error.

The main() function is also responsible for handling command line arguments. These are passed to an application when a user starts an application. Arguments can affect the behavior of an application, making it possible to have a single application doing different tasks by giving it different arguments.

Example of a main() function that prints “Hello World” and exits the program with return code 0.

/*
 * myprogram.c
 */
#include <stdio.h>

int main(int argc, char *argv[])
{
	printf("Hello World\n");
	return 0;
}

Process Command Line Arguments

Arguments are passed to an application when it runs. The example below demonstrates running “myprogram” with 2 arguments (name as a string and age as an integer).

./myprogram cary 25

These arguments are handled in the main() function as:

  • argc: the number of arguments supplied by the user including the application name as well.
  • argv[]: a char * array of variable length, each representing the supplied argument in char * (string).
/*
 * myprogram.c
 */
#include <stdio.h>

int main(int argc, char *argv[])
{
	/* argv[0] = "myprogram" */
	/* argv[1] = "cary" */
	/* argv[2] = "25" */

	/* check the number of arguments */
	if (argc != 3)
	{
		printf("invalid number of arguments. Expect 2\n");
		return -1;
	}

	/* print the arguments - second argument printed as string */
	printf("my name is %s and my age is %d\n", argv[1], atoi(argv[2]));

	return 0;
}

Create User Functions

It is technically possible to include all your program’s logic implementation within the main() function but it is generally not a good idea. This would make the main function so gigantic that it is impossible to understand or debug for a problem. For this reason, we generally split our logic into several functions and have them interact with each other in a neat way. This makes the source code easier to understand and debug. So, how do you define a C function?

A C function can be defined in this format below. If a function does take argument, or return no argument, put void instead.

[return data type] function_name( [argument1], [argument2] ... )
{
	[write logic here]
}

Example 1: a function called addOneToMyNumber() that takes one input argument of type int, add 1 to it and return the result also of type int:

int addOneToMyNumber(int number)
{
	return number+1;
}

Example 2: a function called addOneToMyNumberPointer() that takes one input argument of type int *, add 1 to it but return no result. void is a special keyword that represent nothing is returned or takes no argument in the context of a C function. Pointer is a advanced concept in C programming. Refer to this page to learn more.

void addOneToMyNumberPointer(int * number)
{
	return *number+1;
}

Example 3: a function called printMyInt() that prints the given integer value and returns nothing:

void printMyInt(int number)
{
	printf("number is %d", number);
}

All 3 examples above can be invoked from within the main() function. Also, the main() function can also take void arguments. This means that this program will ignore all arguments given.

/*
 * myprogram.c
 */
#include <stdio.h>

int main(void)
{
	int num = 10;
	num = addOneToMyNumber(num);
	addOneToMyNumberPointer(&num);
	printMyInt(num);
	return 0;
}

Join the conversation

Your email address will not be published. Required fields are marked *