Function call with different datatypes
Isn't line 7 of this program "pay = prt(pay);" supposed to throw a compile or run-time error because it is passing in an int to a param that requ开发者_如何学编程ires a double? I compiled it fine with dev-c++ and ran the program with both lines of output. Please explain, thank you.
#include <stdio.h>
int prt(double b);
main ()
{
int pay = 3;
double tax = 2.2;
pay = prt(pay);
prt(tax);
}
int prt(double b)
{
b *= 2;
printf("%.2lf\n", b);
}
C will automatically convert between different numeric types in this situation.
See Implicit type conversion in C-like languages.
You declared a function as int
but never returned anything, and didn't give main
a return type either. I'd say any compiler would be well within it's rights to reject your code.
data type having smaller or equal size can be converted to higher one.
in reverse case: Float to int causes truncation, ie removal of the fractional part. double to float causes rounding of digit long int to int causes dropping of excess higher order bits.
精彩评论