Why is function main special
If your code adheres to the Unicode programming model, you can use the wide-character version of main , wmain. The main function serves as the starting point for program execution. It usually controls program execution by directing the calls to other functions in the program. A program usually stops executing at the end of main , although it can terminate at other points in the program for a variety of reasons.
At times, perhaps when a certain error is detected, you may want to force the termination of a program. To do so, use the exit function.
See the Run-Time Library Reference for information on and an example using the exit function. Functions within the source program perform one or more specific tasks. The main function can call these functions to perform their respective tasks. When main calls another function, it passes execution control to the function, so that execution begins at the first statement in the function. A function returns control to main when a return statement is executed or when the end of the function is reached.
You can declare any function, including main , to have parameters. The term "parameter" or "formal parameter" refers to the identifier that receives a value passed to a function. See Parameters for information on passing arguments to parameters. When one function calls another, the called function receives values for its parameters from the calling function.
These values are called "arguments. When you want to pass information to the main function, the parameters are traditionally named argc and argv , although the C compiler does not require these names. The types for argc and argv are defined by the C language.
Parameter Passing to functions The parameters passed to function are called actual parameters. For example, in the above program 10 and 20 are actual parameters. The parameters received by function are called formal parameters. For example, in the above program x and y are formal parameters. There are two most popular ways to pass parameters. So any changes made inside functions are not reflected in actual parameters of caller.
Pass by Reference Both actual and formal parameters refer to same locations, so any changes made inside the function are actually reflected in actual parameters of caller. Parameters are always passed by value in C. For example. For example, consider the below program. The function fun expects a pointer ptr to an integer or an address of an integer. It modifies the value at the address ptr. Moreover, if the return type of the function is void, we still can use return statement in the body of function definition by not specifying any constant, variable, etc.
We can get around this limitation by returning pointer to array or pointer to function. In C, it is not a good idea to declare a function like fun. Skip to content. Change Language. Related Articles. Object Oriented Programming. Exception Handling. File Handling. Table of Contents.
Save Article. Improve Article. Like Article. Does C support function overloading? How can we return multiple values from a function? What is the purpose of a function prototype? Static functions in C exit , abort and assert Implicit return type int in C What happens when a function is called before its declaration in C?
0コメント