what data type is the result of a caculation on an int and a double?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • annaaaa
    New Member
    • Jun 2013
    • 3

    what data type is the result of a caculation on an int and a double?

    okay for instance
    Code:
    int x = 4;
    double y = 2;
    
    printf("%d", (x / y + 5));
    what data type is
    (x / y + 5)
    ??
    thanks
    Last edited by Rabbit; Jun 24 '13, 05:11 PM. Reason: Please use code tags when posting code or formatted data.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    double

    In implicit conversions of this type the compiler always converts up to the type that is largest (holds the largest range of values) in order to get the best chance of using a type that can actually hold the answer.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      The int is converted to double and the result is double.

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        Consider line 4: you associate conversion specifier "%d" with expression (x / y + 5). printf does not warn you if a conversion specifier does not match the type of the corresponding argument. You might prefer to introduce an intermediate variable so that it will be easier for you to notice a mistaken conversion specifier. The implicit conversions are well-defined, but you may not find it easy to remember them.

        Code:
        int x = 4;
        double y = 2;
        double z;
        
        z = x / y + 5;
        printf("%d", z);   // wrong
        printf("%f", z);   // right

        Comment

        Working...