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.

Recommended Answers

num3 isn't a float, try using the correct specifier in printf.

Jump to Post

All 2 Replies

num3 isn't a float, try using the correct specifier in printf.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.