1

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);   
}
Lee Louviere
  • 5,162
  • 30
  • 54
wadafarfar
  • 49
  • 2
  • the top answer at http://stackoverflow.com/questions/175689/can-you-use-keyword-explicit-to-prevent-automatic-conversion-of-method-parameters provides an interesting way to enforce type strictness here that leans on templating and private access... – fearlesstost Jul 14 '11 at 17:13
  • Ugh, [dev-c++](http://stackoverflow.com/tags/dev-c%2b%2b/info). – R. Martinho Fernandes Jul 14 '11 at 17:24
  • Not understanding the downvote. The question is easy to understand, and on-topic. Did the downvote occur because the OP doesn't have much education. If that's the case, we should downvote all questions. – Lee Louviere Jul 14 '11 at 17:53
  • @Xaade: This is C code not C++ for one. Second, it is ambiguous and vague. –  Jul 14 '11 at 17:57
  • ambiguous? Why doesn't my code fail to compile when I send an int to a function asking for a double? I think that's pretty standard English there. The fact that the top answer clearly and easily answers the question should point out that the question is easy to understand. All it needs is a simple retag. – Lee Louviere Jul 14 '11 at 18:20
  • @Xaade the top answer answers one of the interpretations. A different answer pointed out that "main" misses a return type and that "prt" misses a "return" statement, and it was downvoted for pointing that out. That's not fair. – Johannes Schaub - litb Jul 14 '11 at 21:48

3 Answers3

6

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

See Implicit type conversion in C-like languages.

RichieHindle
  • 272,464
  • 47
  • 358
  • 399
  • @wadafarfar: Are you serious? This question isn't even tagged java. Even if it was, it wouldn't make up for this poorly designed and poorly worded question. –  Jul 14 '11 at 17:50
3

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.

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • 1
    The first is technically allowed, although you get UB if you try to *use* the value (it was supposed to have) returned. The second requires a diagnostic -- C++ doesn't allow the implicit int rule of older C. – Jerry Coffin Jul 14 '11 at 17:16
  • 1
    In C++ you get UB even if you don't use the value. Just flowing off the end is UB in C++. – Johannes Schaub - litb Jul 14 '11 at 17:36
-1

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.

subhash kumar singh
  • 2,716
  • 8
  • 31
  • 43