How to get the proper output in the C Program?

5640 views c
8

I am doing a simple multiplication in this code. I have initialized integer a = 200000 and b =100000 which is well within the limit of integer range. When I tried to store the multiplied value of a*b in a long integer c. It is not showing the desired result until one of the variables a or b or both are changed into long integer data type. I am using a 64-bit compiler. The sizeof(int) is 4 bytes.

#include <stdio.h>
int main() {
    int a = 200000;
    int b = 100000;
    long long c = a*b;
    printf("%lld",c);
    return 0;
}

I have even tried multiple compilers and the output is -1474836480 which looks like out of range output but I need to know why this thing occurs.

answered question

Please don't tag C questions as C++. They are not the same language.

When you are multiplying two integer they can only hold max value of int else you type case them to long long.

"well within the limit of integer range" - Don't confuse "integer" and int. There are several integer types. int is just one of them.

@cdhowie ... nevertheless in this case the answer is valid for both languages.. you cpuld have left c++ tag instead of C and it would have been ok.

3 Answers

5

long long c = a*b;

a*b is evaluated as the multiplications of two int values. The result of this operation is also an int (causing the overflow) which is then implicitly extended to long long before being stored in c. The problem here is that the extension occurs after the multiplication.

Explicitly extending either a or b to long long before the multiplication will fix this:

long long c = (long long)a * b;

As you've noticed, changing the type of a or b to long long also fixes the problem. It's another way to implement the same solution.

Further reading: Implicit conversions

posted this
9

When you multiply an int and an int, you get an int result. Assigning that to a long doesn't change that.

When you multiply an int and a long, you get an long result.

posted this
9

int a = 200000;
int b = 100000;
long long c = a*b;

a and b are of type int. The values 200000 and 100000 are probably within the range of int on your implementation, though the standard only guarantees 16 bits (-32767 to +32767).

In almost all cases, the type of an expression in C is determined by the types of its operands, not their values or by the context in which the expression appears. In this case, a * b mathematically would evaluate to 20000000000, which would require more than 32 bits to represent. (On a system with 64-bit int, that wouldn't be a problem, but most implementations these days have 32-bit int).

long long is guaranteed to be at least 64 bits wide, so it's well able hold that value -- but you have to give it a value of type long long. As cdhowie's answer correctly says, converting one or both operands to long long should do the trick.

It's hard to tell from this small program, but you should consider the possibility that a and b should have been of type long long in the first place. If you're converting numeric values to wider types, it's sometimes (but by no means always) that the values should have been of that wider type.

posted this

Have an answer?

JD

Please login first before posting an answer.