wadafarfar
wadafarfar

Reputation: 49

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 requires 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);   
}

Upvotes: 1

Views: 191

Answers (3)

subhash kumar singh
subhash kumar singh

Reputation: 2756

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.

Upvotes: -1

Puppy
Puppy

Reputation: 147054

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.

Upvotes: 3

RichieHindle
RichieHindle

Reputation: 281875

C will automatically convert between different numeric types in this situation.

See Implicit type conversion in C-like languages.

Upvotes: 6

Related Questions