Can I use float as return type for main function. In c++. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can I use float as return type for main function. In c++.

Eg.float main(){ Return 9.8 ; } In codeblocks it shows error.

18th Aug 2019, 8:20 PM
ABHISHEK GUPTA
8 Answers
+ 2
No, int is the only valid return type for main. The eax register of the cpu is used as a return value and it cannot store floating point values. ( x86 ) And they are too inaccurate anyway.
18th Aug 2019, 8:35 PM
Dennis
Dennis - avatar
+ 2
No, you cannot. You used to be able to use void main() but that's been gone for a while. Now you can only use int main() which returns an integer value.
19th Aug 2019, 2:04 AM
UrBoyO
UrBoyO - avatar
+ 1
But in Ubuntu float return type for main function is accepted. It runs without any error. Why??
20th Aug 2019, 5:05 PM
ABHISHEK GUPTA
0
Some compilers allow all kinds of return values for main. But anything other than int is not following the standard and is not portable. I'd have to look through the assembly to be sure but it most likely just gets casted to an int. ( or undefined )
20th Aug 2019, 6:23 PM
Dennis
Dennis - avatar
0
ABHISHEK GUPTA Certain compilers will allow it. Most C++ compilers used to have that feature but they started getting rid of it. Nowadays it's just easier and a normal practice to use int main() vs. anything else.
20th Aug 2019, 8:38 PM
UrBoyO
UrBoyO - avatar
0
Can I run graphical program in codeblocks?
21st Aug 2019, 11:37 AM
ABHISHEK GUPTA
0
No, because when you return 0 in the main function, you are saying that que programm exits with no errors
21st Aug 2019, 4:17 PM
Daniel Rodríguez
Daniel Rodríguez - avatar