This is a trivial program in Turbo C 2.01. It is intended to show that C can do arithmetic with mixed data types, and what the results are:

#include <stdio.h>

void main(void)
    int num1 = 5;
    float num2 = 2;
    int num3;

    num3 = num1 / num2;
    printf( "%f\n", num3);

I would expect it to print 2, or maybe 3, but it prints 0.0000, as if the division failed. It does not generate any warnings or errors when compiled.

This is not what the book says it will produce, and the book was written for this version of Turbo C.

If I declare num3 as a float, it prints out 2.50000, which is what I would expect.

Did I do something wrong, or am I misunderstanding something?

Environment is Borland Turbo C 2.01 on Windows XP Pro SP3.

4 Years
Discussion Span
Last Post by rory.starkweather.7

Ah. I jumped to a conclusion. Before the code snippet there is a piece of text that says:
int num1 = 5;
float num2 = 2;
int num3;

I assumed that the declarations in the snippet would be the same, but they aren't.

In the snippet num3 is delared as a float.

Works fine now.

Thank you.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.