Main Function In C

Main Function In C

In C, C++, and Java, the main function is crucial as it serves as the entry point of a program. When a program is executed, the operating system begins execution by running the statements within the main() function. It is a user-defined function and is mandatory for program execution.

Syntax of C main() Function

return_type main() {
    // Statement 1;
    // Statement 2;
    // and so on..
    return;
}

We can write the main function in many ways in C language as follows:

int main(){} or int main(void){}

Important Points about C main Function

  • The main function is where a program’s execution begins.
  • Every program has exactly one main function.
  • Its name should be “main,” not anything else.
  • The main function typically returns an integer value or void.
  • The main function is called by the operating system, not by the user, to start the program’s execution.

Types of C main Functions

Main Function with No Arguments and Void Return Type:

  • This is a standard form of the main function in some programming languages like C++.
  • It doesn’t take any arguments.
  • It doesn’t return a value (void).

Main Function with No Arguments and Int Return Type:

  • Another common variation of the main function in languages like C and C++.
  • It doesn’t take any arguments.
  • It returns an integer value.

1. Main Function with No Arguments and Void Return Type

It appears you want to see an example of a simple C program with the main function that prints a string using the printf function. Here’s an example in C:

Syntax

void main()
{
   // Function body
}

This given function is equivalent to

void main (void)
{
    // Function Body
}

Example

// C Program to show main with no return type and no
// arguments
#include <stdio.h>
 
// Defining a main function
void main()
{
 
    // code
    printf("Hello Skillvertex!");
}

Output

Hello Skillvertex !


2. Main Function with No Arguments and int Return Type

In the example, the int return type in the main() function signifies the program’s exit status. By convention, a return value of 0 indicates successful completion, while any other value indicates an error during execution.

Syntax

int main()
{
   // Function body
}

Example



// C Program to show the main function with int return type
// and no arguments
#include <stdio.h>
 
int main()
{
    printf("Hello Skillvertex!");
    return 0;
}

Output

Hello Skillvertex!

3. Main Function with the Command Line Arguments

In this example, we use command-line arguments in the main() function. argc stands for argument count and stores the number of arguments passed in the command line. By default, its value is 1 when no arguments are passed. argv[] is a char pointer array that stores all the command line arguments provided. When running the program without any command line arguments, argc is 1, indicating no additional arguments were passed.

Syntax

int main(int argc, char* argv[])
{
   // Function body
}
// C Program to illustrate the main function with command line arguments
#include <stdio.h>
 
int main(int argc, char* argv)
{
 
    // printing the coundt of arguments
    printf("The value of argc is %d\n", argc);
    // prining each argument
    for (int i = 0; i < argc; i++) {
        printf("%s \n", argv[i]);
    }
 
    return 0;
}

Output

The value of argc is 1
./369df037-e886-4cfb-9fd4-ad6a358ad7c6

FAQ- Main Function In C

Q1. What is the main function in the C example?

Ans. In C, main is a predefined function and a special entry point for every C program. It is responsible for starting the program’s execution and terminating it. The main function has int or void as its return data type and is where the program code begins execution.

Q2. How to use main () in C?

Ans. The main function in C is the program’s starting point and is executed by the operating system. It begins with an opening bracket ‘{‘ and ends with a closing bracket ‘}’. The main function can have a return type of ‘int’ or ‘void’.

Q3.What are the 4 types of functions in C?

Ans. Function with no arguments and no return value.
Function with no arguments and a return value.
Function with arguments and no return value.
Function with arguments and with a return value.

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

Leave a Comment