predominant Operate in C – GeeksforGeeks

predominant Operate in C – GeeksforGeeks

The primary operate is an integral a part of most programming languages akin to C, C++, and Java. This operate is the entry degree of a program the place the execution of a program begins. It’s a user-defined operate that’s necessary for the execution of a program as a result of When a C program is executed, the working system begins executing this system on the predominant() operate.

Syntax of the primary operate in C:

return_type predominant(){

     // Assertion 1;

    // Assertion 2;

    // and so forth..

return;

}

We will write the primary operate in some ways in C language as follows:

int predominant(){} or int predominant(void){}
predominant(){} or void predominant(){} or predominant(void){} or void predominant(void){}

Within the above notations, int means integer return sort, and void return sort means that doesn’t return any info, or (void) or () means that doesn’t take any info.

Key factors about the primary operate:

  • It’s the operate the place this system’s execution begins.
  • Each program has precisely one predominant operate.
  • The title of this operate ought to be “predominant” not anything.
  • The primary operate all the time returns an integer worth or void.
  • The primary operate known as by OS, not the consumer.

Sorts of Major Operate

  1. Major operate with void return sort
  2. Major operate with int return sort
  3. Major operate with the argument
  4. Major operate with a couple of argument and int return sort
  5. Major operate with a couple of argument and void return sort

1. Major operate with void return sort 

Let’s see the instance of the primary operate inside which we have now written a easy program for printing a string. Within the under code, first, we embody a header file after which we outline a predominant operate inside which we write an announcement to print a string utilizing printf() operate. The primary operate known as by the working system itself after which executes all statements inside this operate.

C

#embody <stdio.h>

  

void predominant()

{

  

    

    printf("Good day Geek!");

}

2. Major operate with int return sort 

On this instance, we use the int return sort in the primary() operate that signifies the exit standing of this system. The exit standing is a means for this system to speak to the working system whether or not this system was executed efficiently or not. The conference is {that a} return worth of 0 signifies that this system was accomplished efficiently, whereas another worth signifies that an error occurred.

C

#embody <stdio.h>

  

int predominant()

{

    printf("Good day Geek!");

    return 0;

}

3. Major operate with the argument

On this instance, we have now handed some arguments in the primary() operate as seen within the under code. These arguments are referred to as command line arguments and these are given on the time of executing a program. The primary argument argc means argument rely which implies it shops the variety of arguments handed within the command line and by default, its worth is 1 when no argument is handed. The second argument is a char pointer array argv[] which shops all of the command line arguments handed. We will additionally see within the output once we run this system with out passing any command line argument the worth of argc is 1.

C

#embody <stdio.h>

  

int predominant(int argc, char* argv[])

{

  

    printf("The worth of argc is %dn", argc);

  

    for (int i = 0; i < argc; i++) {

        printf("%s n", argv[i]);

    }

  

    return 0;

}

Output

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

Now, run the applications within the command immediate or terminal as seen under screenshot and handed any arguments. predominant.exe is the title of the executable file created when this system runs for the primary time. We handed three arguments “geeks for geeks” and print them utilizing a loop.

Main Function in C Language

 

4. Major operate with a couple of argument and int return sort

This operate is just like the primary operate with an int return sort however this operate signifies that the operate doesn’t take any arguments. On this instance, the primary operate is just like the int predominant(void ) which exhibits that it returns an integer worth with out passing any arguments to this operate.

C

#embody <stdio.h>

  

int predominant(void)

{

  

    printf("Hello Geeks!n");

    return 0;

}

5. Major operate with a couple of argument and void return sort

This operate is just like the primary operate with a void return sort however this operate signifies that the operate doesn’t take any arguments. On this instance, we have now outlined the primary operate because the void predominant(void ) which exhibits that it doesn’t return any worth and in addition we aren’t passing any arguments to this operate.

C

#embody <stdio.h>

  

void predominant(void) {

  

    printf("Hello Geeks!n");

      

}