What is the main in C?In this topic, we will discuss the main in C programming language. A main is a predefined keyword or function in C. It is the first function of every C program that is responsible for starting the execution and termination of the program. It is a special function that always starts executing code from the 'main' having 'int' or 'void' as return data type. In other words, a main() function is an entry point of the programming code to start its execution. Importance points of the main() function
SyntaxProgram to print a statement using main() functionLet's consider a program to print a statement without using the void and int main() function in C. Program.c Output: Welcome to the JavaTpoint Program to call nested function using main() functionLet's consider a program to call nested function inside the main() function. Main.c Output: It is a main() function Finally exit from the main() function. Types of the main() functionFollowing are the types of the main() function used in C
void main() functionA void is a keyword that references an empty data type that has no return value. In other words, the void data type is used when we don't want to return any value to the calling function. Furthermore, it is used with the main() function to return nothing and can be used with user-defined and predefined functions in C programming. Syntax Program to demonstrate the void main() functionLet's write a simple program to demonstrate the void main() function. void.c Output: C is a programming language. It is a void main() function in C. int main() functionAn int is a keyword that references an integer data type. An int data type used with the main() function that indicates the function should return an integer value. When we use an int main() function, it is compulsory to write return 0; statement at the end of the main() function. The return 0; statement represents that the program has been successfully executed, whereas any other statement represents the unsuccessful termination of the program. Syntax Program to return a value using the int main() function in CLet's write a program to return a value using an int main() function in C language. prog.c Output: Welcome to the JAVATPOINT It is an int main() function to return a value. Program to print the iterative numbers using int main() functionLet's consider an example to display the iterative sequence of number in C using int main() function. Program.c Output: 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 We can also use an EXIT_SUCCESS statement, which is the alternative statement of the return 0. If the program is not successfully executed, we can use the EXIT_FAILURE in the absence of the return 1 statement. The definition of the return statement in the standard input-output header file (stdio.h), whereas the EXIT statement is defined in the standard library (stdlib.h) header file. Program to use the EXIT_SUCCESS statement in int main() functionLet's write a program in C to use the EXIT_SUCCESS statement in replacement of the return statement. Exit.c Output: Hello, Welcome to the World Use EXIT_SUCCESS on successful execution of the program Note: A return statement cannot be used with the void main() function because it return a value. Therefore, we can't use it with void main() function. However, we can use the EXIT statement with the void main() function.int main (int argc, char *argv)A main() function can be called using command line arguments. It is a function that contains two parameters, integer (int argc) and character (char *argv) data type. The argc parameter stands for argument count, and argv stands for argument values. int main(void) functionAn int main(void) function is similar to the int main() function to return an integer value. But we can pass more than one argument to the int main(), whereas the int main(void) can only be called without any argument. Program to use the int main(void) function in CLet's consider a program to demonstrate the int main(void) function in C language. prog_main.c Output: Welcome to the JAVATPOINT void main (void) functionA void main (void) function is similar to the void main() function that does not return a value. However, the void main() function can accept multiple parameters, but it does not return a value. It is an empty data type, whereas the void main(void) does not take any parameter because it has a predefined main(void) function. Program to use the void main(void) function in CLet's consider a program to demonstrate the void main(void) function in C language. Main_prog.c Output: Welcome to the JAVATPOINT Next TopicCalculator Program in C |