main fucntion

why we should use only integer or void for main function
why we can't use double or float or other data type for main function
i don't know and i did try but compiler was saying that main function should be int.
please if anyone can help me through this question then i will be helpful ! thanks in advanced !
Last edited on
closed account (SECMoG1T)
I guess

The int returned by main just signifies a status of the execution and exit and each single value returned have a particular status whith which it is associated such as successful execution, exit with an error etc.
Last edited on
Thank u for ur reply.
But if int does that work then string and double and long double can do the work .... i mean when we write a function (not main). WE can write double and string before the function name as like as we can write void and int but why we can't write double or string or other data type or our created data type with main function except int or void ?
Last edited on
why we should use only integer or void for main function
only int. void main is non-standard and unsupported.

why we can't write double or string or other data type or our created data type with main function except int or void ?
Because OS expects int and would be surprised if something else was passed.

WE can write double and string before the function name
Try to change function return type without changing anything in main
1
2
3
4
5
6
7
8
9
int foo()
{
    return 42;
}

int main()
{
    int i = { foo() };
}
yeah u are right ! but sir .I think u haven't understood my question !
so look it here
if i am writing
void main () or int main () then it is right but
if i am writing float main () or double main () then compiler is showing me error?
i want to understand the basic logic behind this problem. if i am writing int main () and return something char or float number it transfer the value from other data type to int .....but my main question is why and why only why i have to write int or void for main
what is wrong with the other function ?
bcz i can write other functions with string double and other data types then what is the problem or logic behind this problem ?
:(
When you define a function, say for example,
double my_func(int x, int y);
You expect that function to always accept two ints and return a double.
"main" is a special function, that has been pre-defined for you, as
int main(int argc, char *argv[]);
you cannot change the definition!
When you run your program, the operating system calls your "main" function for you. It will look for a function called "main", that takes an int and an array of char *. It will call this function, passing those parameters, and expect to get an int return. If it doesn't find such a function to call, it cannot run your program.
This is the interface to the operating system, and the compiler enforces it. If you want a different kind of "main", you have to use or create a different compiler/operating system combination.
Think about it like a key fitting into a lock. You can't just make up your own key, if you do, you also have to find or make up a lock that it will fit, and vice versa.
if i am writing float main () or double main () then compiler is showing me error?
Yes, because main should return int. OS expects int. Standard requires that you return int. That is all. There are another functions which calls main and they expect that you return int. If you return something else, overlaying runtime will break and something unexpected can happen. Like BSoD.

bcz i can write other functions with string double and other data types then what is the problem or logic behind this problem ?
Because you are writing other functions which uses that function and can change them at the same time as you change your function.

In case of runtime, you cannot change it. Same problem can arise if you are using external library, they too expect specific functions as callbacks and you cannot change either argument or return type.

For example try to change return type in function foo() without changing chunk of code below:
1
2
3
4
5
6
7
8
9
10
11
int foo()
{
    return 42;
}

/*↓↓↓DO NOT CHANGE↓↓↓*/
template<typename T> void bar(T)
{ typename T::__notype__ baz; }
void bar(int) {}
int main() { bar(foo()); }
/*↑↑↑DO NOT CHANGE↑↑↑*/

Last edited on
Thank u both of u !
and this conversation has removed everything .
Now i have fully understood the reason !
Thank u very very much that u have replied on this issues !
May Allah Bless u ! Thank u again ^_^
Topic archived. No new replies allowed.