juanli
juanli

Reputation: 613

data type confusion in C

The following code works:

int main(void)
{  
   float f = get_float();
   int i = round(f*100);
   printf("%i\n", i); 
}

Yet, error generated if coding this way:

printf("%i\n", round(1.21*100));

Output says round(1.21*100) is float. So, then why

int i = round(f*100); 

is fine?

Upvotes: 5

Views: 770

Answers (3)

user2371524
user2371524

Reputation:

This is a bit of duplication, but maybe helps for a better understanding:

  1. round() has the following prototype:

    double round(double x);
    

    so it returns double.

  2. There is an implicit conversion from double to int in C, so writing

    int i = round(f*100);
    

    will convert the result of round() to int.

  3. If you have a function that expects an int, e.g.

    void printMyNumber(int number)
    {
        printf("%i\n", number);
    }
    

    you can call it like this:

    printMyNumber(round(f*100));
    

    and the implicit conversion works as expected, because the compiler sees both types (the return type from round() and the expected argument type of printMyNumber()).

  4. The reason this doesn't work with printf() is that the prototype of printf() looks like this:

    printf(const char *format, ...);
    

    so, except for the first argument, the types of the arguments are unknown. Therefore, whatever you pass is passed without any conversion (except for default argument promotions). Of course, you could use a cast to achieve an explicit conversion instead:

    printf("%i\n", (int) round(f*100)); // <- this is fine
    

Upvotes: 4

Some programmer dude
Some programmer dude

Reputation: 409136

When you do

int i = round(f*100); 

you convert the result of the double function round. The converted result is stored in the int variable i, which can be used with the format "%i" as it expects an int argument.

When you pass the double result of round directly as an argument to a format that expects an int you have mismatching format and argument types. That leads to undefined behavior.

No conversion is made in the call to printf, and no conversion can be made since the code inside the printf function doesn't know the actual type of the argument. All it knows is the format "%i". All possible type-information is lost for variable-argument functions.

Upvotes: 16

Darshan Prajapati
Darshan Prajapati

Reputation: 843

This is because of the behavior of automatic type casting. In printf, automatic typecasting does not work. When you say %i, it simply expects integer, it can not convert double to integer and then print.

In assignment operation, double is converted to integer first and then it is assigned to left operand of the = operator. I hope this helps.

Upvotes: 5

Related Questions