Why main( ) function in c is important | Difference between int main( ) and void main( ) in c
Why main function in c is important
Here, you will get the correct technical reasons, why main function in c is a starting point of any programming languages.
All C language programs must have a main() function. It’s the core of every program. It’s required. The main() function doesn’t really have to do anything other than be present inside your C source code. Eventually, it contains instructions that tell the computer to carry out whatever task your program is designed to do.
The basic main() function
When the operating system runs a program in C, it passes control of the computer over to that program. This is like the captain of a huge ocean liner handing you the wheel. Aside from any fears that may induce, the key point is that the operating system needs to know where inside your program the control needs to be passed. In the case of a C language program, it’s the main() function that the operating system is looking for.
Note that the basic, simple main()function doesn’t require a specific keyword or procedure for ending the program. In some programming languages, an END or EXIT command is required, but not in C. In the C language, the program ends when it encounters the last brace in the main() function. That’s the sign that the program is done, after which control returns to the operating system.
The main() function has two arguments that traditionally are called argc and argv and return a signed integer. Most Unix environments expect programs to return 0 (zero) on success and -1 (negative one) on failure.
Argument Name Description
argc Argument count Length of the argument vector
argv Argument vector Array of character pointers
The Main Function. In C, the "main" function is treated the same as everyfunction, it has a return type (and in some cases accepts inputs via parameters). The only difference is that the main function is "called" by the operating system when the user runs the program.
The main () function as a built-in function:
The compilers of most of the programming languages are so designed that the main () function constitutes the entry point of the program execution. It defines the point from which the program has to start executing itself though there are many other sub-routines and other user-defined functions included in the program.
The main () function is the controlling section of our code because even though the control of the program is shifted to the UDF (user defined function) during the program execution after a function-call from main (), once it's execution is completed, the control is transferred back to the main () function with some or no return value (as in the case of a void function).
The main () function provides a platform for calling the first user-defined function in the program.
It has got its own functionality and structural features with respect to the usage of syntaxes which cannot be changed by the end user unless he writes his own compiler. But the UDF's have functions and structures designed by the user or programmer.
The main () has function definition (the code of a function) but it doesn't have any function declaration. Though we often use int main () or void main (), these declarations are not compulsory. But a UDF should have such declarations.
The main () function as a User-defined function:
The main characteristic of the UDF's is that the function definition is entirely given by the programmer and it's also true that in every program we write; the code inside the main () function is absolutely different from the other. If at all it was a built-in function then the source code in the main () function would have remained the same in all the programs.
The main () function is never declared in any other sub-routine before calling it and this gave a rise to what is called as the scope of variables and functions as 'local' and 'global', which is undoubtedly a user defined and user-wished context. Rather main () function acts as a reference section for the user to define a local or a global variable/function.
The main () function can also be sent with arguments while it's execution like that of a UDF which are referred to as formal parameters or dummy parameters.
The conclusion that can be drawn is that even though main () function is undoubtedly a built-in function, it cannot rule out the fact that, it also satisfies all the conditions and constraints of a user defined function. With respect to the instant of time and context of usage we can go on referring it to as built-in function or user-defined function interchangeably.
Thanks
Mahesh V Kondawar
(Trainer)
encodetraining@gmail.com
https://www.encodetraining.com


Comments
Post a Comment