saving . . . saved Regarding Assignment has been deleted. Regarding Assignment has been hidden .
Regarding Assignment
Why is the following code wrong? Why can't the main function return float data type? What to do if we have to find the difference between two floating numbers?
Please explain.
<span style="background-color: rgb(250, 250, 250);">
<span style="background-color: rgb(250, 250, 250);">#include <stdio.h></span>
<span style="background-color: rgb(250, 250, 250);">
void difference(float a, float b)
   float c = a-b;
   printf("The difference between x and y is %f\n", c);

float main()
   float x,y;
   printf("Enter the number 1:");
   scanf("%f", &x);
   printf("Enter the number 2:");
   scanf("%f", &y);


   return 0;

C-and-Cpp Scope-Of-Variables 08-09 min 40-50 sec 21-10-20, 11:02 a.m. bsscharan


I believe this program is working fine. I executed this program on my end with the inputs given below: 

Enter number 1: 3.56
Enter number 2: 1.782
The difference between x and y is 1.778000

In case you are referring to some other point, please let us know. 

21-10-20, 2:48 p.m. sudhakarst
I am getting a warning that the main cannot be float when I run the code. But for me also, the program is running.
 The warning is:
main.c:9:1: warning: return type of 'main'
      is not 'int' [-Wmain-return-type]
float main()
main.c:9:1: note: change return type to
float main()
1 warning generated.

After this code is running normally.
 Please check what the problem is.
Also, I ran the code on , not a offline compiler.

21-10-20, 3:22 p.m. bsscharan
In C/C++ standard, the entry point must be one of the following forms:
int main(void);
int main();
int main(int argc, char **argv);
int main(int argc, char *argv[]);

If we have an entry point other than this, we will get a warning, depending upon the compiler's rules. For more, click 
21-10-20, 3:27 p.m. sudhakarst
So, it might not be a problem in some other compiler, right? 
21-10-20, 4:53 p.m. bsscharan
I guess you will always get a warning since C/ C++ standard accepts int main() as an entry point.  
21-10-20, 4:55 p.m. sudhakarst
Even though I get a warning, the code will run properly, right?
21-10-20, 5:15 p.m. bsscharan
Yes, we can ignore warnings. 
21-10-20, 5:51 p.m. sudhakarst
Thank you.

21-10-20, 5:56 p.m. bsscharan

Login to add comment

Log-in to answer to this question.