what is the importance of main() in c-language
I will try to answer both of your question in here, to avoid writing twice similar concepts. The way I see it (and take this with a grain of salt, since it's both an oversimplification and a half-truth), I think of the main function as the building block for the stack memory when the program is running. It's the cornerstone, where the program begins and where it ends. Visually: http://lh5.ggpht.com/_VrsVJGFhz4c/SprO3yHjvWI/AAAAAAAABq4/NIdIKWF38o4/stack_heap%5B4%5D.png Any call to a function will add it on top of the stack, and when a function is done, the stack will be removed. The program will terminate when the main frame resolves, when the stack is removed for main. You can't really write a "program" so to speak without main, in the sense that the program won't run, nor it will produce an output. What can be done is to write it as a library, or as object files (foo.o). Suppose I want to implement a singly-linked list in C. Instead of writing everything in a single file, another way would be to write a file defining the structure and operations. It would contain a struct with a pointer and the variable you want to store, traverse, insert, delete and find operations. Then you would #include the file in the interface file (the file with the main function). This way of writing code both increase the abstraction, allowing you and others to utilize the library written, but also is a more secure way to writing it. Hope I wasn't too prolix and unclear. Luckily, something I wrote will make sense and be right :-)
Join our real-time social learning platform and learn together with your friends!