What does main() function do in?wtz da difference between void main() and int main()?why should I use?

extra two questions :-
what first two lines,

include <iostream>

using namspace std;
do?

thanks

Recommended Answers

All 20 Replies

well, the main() function, the int main() and the void main() all do the same, which is process the main process of the program. The difference is int main() is the only right way of calling it, since every program, when called, returns an "error message" that the OS interprets, in which case, closing the program with a return 0; tells the process calling your program that it ended without a problem.

As for include <iostream> and using namespace std;, the first one calls the library to be used in the program... There's A LOT of libraries you can use, and there's infinite possibilities of libraries you can create to use in your program. The second one declares the namespace to be used from the libraries you are calling, in this case, the std namespace of the iostream library.

difference between int main and void main is that int main has int return type while void can have any type of data type as return type.
# include <iostream > is a pre processor type which includes iostream header file to ur programe so that u can use the cin and cout function which are already defined in iostream

firdousahmad indeed, but the difference between int main() { return(0);} and void main(){} is alot more than the return type.
Read more about this question here.
#include <iostream> includes in your program the library iostream (Standard Input / Output Streams Library). Read more about that
here.
About the using namespace std;: you can read from here.
A quick search on google would have solved all your problems:d.

I have read thos from the internet. needed more information. int main() related first link,actually exaplians nothing! . I have read some others too but still don't get it. I mean how can we tellif a program was successful or not just by puting return(0) ?even without running it. it could give errors after running.

I have read before that <iosream> link you gave too. not pretty clear about it. I am expecting someone with a beter knowledge to explain. like ,what is a stream .those examples are not good for me. and why do we need a function to deal with input.output and what is actually IN it

return (0) in int main is the value that is returned to the os. Traditionaly, 0 means that everything worked ok. (1) means that there was something wrong.

I have read before that <iosream> link you gave too. not pretty clear about it. I am expecting someone with a beter knowledge to explain. like ,what is a stream .those examples are not good for me. and why do we need a function to deal with input.output and what is actually IN it

For this you really need to read some books about Computer Science, and how things work under the hood. We have a good topic on that metter: Click Here

(1) means that there was something wrong.

While 1 can be an error code, the whole point of using 0 to signify success is because there's only a single success value but a large number of potential error values: anything in the range of int that's not zero.

So a more accurate description is that 0 is a success code sent to the C++ runtime while non-zero is an implementation-defined error code. The only portable values are 0, EXIT_SUCCESS (which is basically defined as 0), and EXIT_FAILURE. EXIT_SUCCESS and EXIT_FAILURE are both defined in <cstdlib>.

I still don't get it. Isn't it up to the OS to decide whether it was successful or not?
how can we tell if the program has errors or no errors by simply putting 1 or a 0. I mean if it has a error, it will stop. if not,it will not. maybe the programmer is not even aware of some small error .how can the programmer put a success or failiure thing? Or is this about a totally different thing?

It all depends on how you code your program. If you have mechanisms in place in your code to handle error than you could try recover from that error. If that is not possible, than you could end the program gracefully and tell the OS that the program did not end successfully. Look up Try … Catch blocks to see what I mean about error handling. One example I can think of for this is an installation program. The program that is installing the software is chugging along when it runs into a problem that it can’t recover from. Wouldn’t it be nice if the installer could tell the OS that hey I could do what I was supposed to do so your new software isn’t going to work? Well that is exactly what you can do with a return statement. In the part of the code that is handling the error can’t fix it than it can return EXIT_FAILURE to the OS so that it know something is wrong. Now the OS know that the software won’t work and will give you options accordingly.

Or is this about a totally different thing?

Yes, totally different. You're thinking of fatal errors that halt execution without any chance for recovery. The return value from main() is for a graceful exit. Let's say you try to allocate memory and it fails. That's an error that can be caught and potentially recovered from, but if it can't be recovered from, exiting with an error status is reasonable:

#include <iostream>
#include <new>      // For bad_alloc
#include <cstdlib>  // For EXIT_FAILURE

using namespace std;

int main()
{
    try
    {
        int* mem = new int[12345];

        ...

        delete[] mem;
    }
    catch (bad_alloc const& ex)
    {
        cerr << "Error allocating memory: '" << ex.what() << "'\n";
        return EXIT_FAILURE;
    }
}
commented: that explains .....well .nice example +2

Is it just me or do they say that both, "include <iostream>" and "namespace std;" are used to call the cout cin functions. both of those parts have something to do with cin cout. I am counfused .any explanation would be appreciated

The #include iostream directive includes the iostream library. Putting the using namespace std; line tells the compiler that further in your program you are using the namespace std, in which the entities from the library are located.
Think of it like this: you want an apple and your mom gives you the bag. In order to get the apple you have to take the bag, reach for an apple and take it. This is how namespaces work. The namespace is the bag, in which entities are located. You want the function cin so you put the using namespace std; to notify the compiler that you are using that bag.
There is another way so that you don't include that namespace, and that is of putting the keyword std:: in from of every iostream library functions, as std::cin or std::cout etc.
So, using namespace std; is a shortcut for not putting std:: in front of every library entities.
More about namespaces you can find here.

still,can anyone tell me more about the "connection" between <isostream> and namespace std;

Here, take a look at this internal thread: Click Here
Also, almost every (if not all) C++ entity is located in the std namespace.

that thread has a great example! thanks

#include <iostream>
using namespace std;
cout <<"enter the name"<<endl;
void main()

why is that ,when I put "cout <<"enter the name"<<endl;" ,before the main() function ,it is not working?

I mean iostream and namspace std together ,decalres cout right? why doesn't it work outside the main()?

If I get this correctly(the relationship between iostream and std) , 'std' is in the iostream and cout/cin like operations are in 'std'? Just want to know the relationship between these too. I am new to C++ and want to know know everything .thanks!

another question. when I put

system("pause");

after the

void main()
{

}

system("pause");

main() like this,the 'system' part is not working too.

why?

sorry if i'm asking too many questions :/

Asking questions isn't a problem. Answering them is what we're here for, after all.

The answer to both of those questions is the same: In C++, all executable statements have to be inside of a function. Declarations and directives do not fall into this category, but function calls do. In general, anything that causes an action to occur, or creates a result, has to be inside a function to be valid.

As for the issue of the std namespace, it's a bit more complicated than that. Namespaces can cover multiple compilation units and multiple headers, so it isn't really correct to say that std is 'inside' of <iostream>. This becomes evident when you learn about other classes and objects in std which are declared inside other headers. For example, the string class is also in std, but it's declaration is in the <string> header file.

ha complicated. Maybe I will get to a chance to learn about them late in the c++ course. this cleared many problems. thanks for helping Schol-R-LEA,Nichito,Lucaci Andrew,deceptikon,NathanOliver,firdousahmad for helping

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.