Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

why we dont use float main() in place of int main().

0 votes
asked Oct 25, 2020 by Mayank Mayank (120 points)

2 Answers

+1 vote
answered Oct 26, 2020 by xDELLx (10,500 points)

In "c" code  should compile without issues.

However C++ should flag this as an error.

in "int main" the int part sends a return value indicating the return status of the code executed.

typically "return" statement value at end of the main will be propagated back to OS & finally to us showing whether the code executed successfully or not.

if you use linux ,u can check the exit status of the last app run usinf "echo $?"

//Simulating code with issue, by doing illegal division, in real world almost anything could go wrong
#include <stdio.h>
int main()
{
    printf("Hello World");
    int x = 1/0; // It is illegal to divide by 0,
                //lets assumse numerator & divisor are returned
                //based on some computation done elswhere & their values returned to us
    return 0;
}
//Exit status is 136 , if all goes ok we return 0 & would expect exit status as 0.



/*****************************************************************
*****************************************************************/
//Good code
#include <stdio.h>
int main()
{
    printf("Hello World");
    return 0;
}
//Exit code is 0 , if all is ok we return 0 ,which differs from above example.

Now if u change the return value to float (or other than int), the OS would be confused as to what value was returned when trying to read the value & always report an error even if the code was executed without issues.

#include <stdio.h>
float main()
{
    printf("Hello World");
    return 0;
}
//...Program finished with exit code 11
0 votes
answered Oct 26, 2020 by Muhammad Awais Raza (190 points)
Because float is use for point value like(2.3 or 0,75) and int use for all real or imaginary numbers like(23575 or -474993)
commented Oct 30, 2020 by Peter Minarik (86,580 points)
Not exactly.

The name indicates its usage: float to use for *floating point numbers* or real numbers, decimal numbers and fractions.

int is used for *integral numbers*, that is, no fraction parts. (Both real numbers and imaginary numbers are broader categories than integral numbers. So int cannot really store them. Also, C/C++ doesn't have a built-in type for imaginary numbers. Many languages do not.)
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...