answersLogoWhite

0


Best Answer

Any program source has to identify a memory location as the start of the program. The OS transfers control to this point of the program or module.

In case of C programs, the starting module is NOT your program, but another initialisation routine, which has name lis c0s.obj, c0l.obj etc. This routine after doing initial housekeeping work, like storing program name, arguments, environment variables etc. in the stack, calls a routine by name main.

How else are we going to tell the OS where to start ?

At the end of executing the main() function, the control goes back to the routine from which the main was called and then to OS, which terminates the program.

User Avatar

Wiki User

13y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago

main function is the function which comes under user defined function through which we can execute a program .That means without main function we cann't execute a programThough main function is the user defined function but it has the highest priority to execute a program.But we can execute a function without main by using the macro #pragma

This answer is:
User Avatar

User Avatar

Wiki User

6y ago

The global main() function defines the entry point of the application. Typically, the main() function is used to parse command line switches and pass control to an appropriate function which will eventually return control to the main() function. When we exit the main() function, the program terminates. The program can also be terminated by invoking either the abort() or exit() functions from within any function. This is usually done when an unrecoverable error is encountered.

This answer is:
User Avatar

User Avatar

Wiki User

8y ago

The main function defines the entry point of your C program. That is, when you execute your program, control is passed to the main function. Every C program must have one (and only one) entry point. When the main function terminates the program itself ends.

The main function has the following protocols:

int main (void);

int main (int argc, const char* argv[]);

int main (int argc, const wchar_t* argv[]);

Where:

argc - argument count

argv - argument value(s)

If you do not require command line arguments, use the first protocol, otherwise use the second. If you require wide characters (UNICODE environments), use the third version.

When using command-line arguments, the argv argument represents an array of pointers to null-terminated strings where argv[argc] is a null pointer (NULL). Also, the first argument (argv[0]) is always the fully-qualified path to the executable. Thus if you invoked the following command:

notepad /P "my file.txt"

The arguments will be as follows:

argc = 3

argv[0] = "c:\windows\notepad.exe"

argv[1] = "/P"

argv[2] = "my file.txt"

argv[3] = NULL

Note that the command-line parser uses whitespace to delimit each argument unless the argument is enclosed in double quotes.

The main function must return an integer (int) even if your operating does not use it. Although you can return void rather than an int, your code will not be portable.

The return value is typically used to indicate an error condition where the value zero indicates no error.

Windows does not use the return value, however you can still test the return value by testing the ERRORLEVEL from a command script or batch file:

@ECHO OFF

foo.exe IF ERRORLEVEL 2 GOTO Level2

IF ERRORLEVEL 1 GOTO Level1

EXIT

:Level1

ECHO The program terminated with error 1

EXIT

:Level2

ECHO The program terminated with error 2

EXIT

Note that error levels must be tested in reverse order because ERRORLEVEL 1 really means ERRORLEVEL>=1 rather than ERRORLEVEL=1 as you might (reasonably) expect.

This answer is:
User Avatar

User Avatar

Wiki User

15y ago

main function consists all the library functions and headers which are used while writing the code

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

The main function is the function that holds all the code that runs the program (the code that controls what we see in the program)

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

It is an entry point function, which is the first user-defined function to be called when your program runs.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

The main function is where the program begins. It should have a return type, int, to check for failure or success.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the Use of main function in c language?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Can you write any c prigramme in c language witthout any function?

No. At minimum, you need to provide a main() function.


What is the use of gets function in c language?

Nothing, it is a security hole. Use 'fgets' instead.


What is the differnce between ordinary function and template function of c language?

The c language does not have template functions. That is a c++ thing.


What is the function of void main in c language?

Actually, it is:int main (void)orint main (int, char **)the point where the execution of the program begins


Where does c language program begin execution?

The execution of the program starts with function main, wherever it is in the source.

Related questions

Can you use the float as a return type of the main function in 'C' language?

No, it should be int type or void.


Why you use main function in c?

Because if you donot use main function in c program, the compiler willnot execute the program.C compiler starts execution from main function itself.


Where execution does take start in C language?

It start with function - main()


Can you write any c prigramme in c language witthout any function?

No. At minimum, you need to provide a main() function.


Can you use main function as a recursive function in C?

Yes


Why you use 'integer' before function in C language?

To specify the return-type of the function.


What function is used to perform exponentiation in C language?

You can use the pow() function in math.h.


Why the main function is called userdefine function in c language?

It's called 'main' by tradition; this feature lets the linker to know, where to start the execution.


Main function in c?

The main function defines the entry point of an application in C.


Why do you use void main in C language?

it returns nothing


Why you use else function in c language?

You cannot have a function named else, because it is a reserved word.


How you can convert from string to int in C language?

Use the atoi() or atol() function.