Log in

View Full Version : int times long?


baop858
27-01-2008, 19:46
What happens when I multiple an int by a long, is the result an long or an int?

Kyle Fenton
27-01-2008, 20:00
Depends, what did you declare your answer variable to be?

int answer

a * b = answer
would result in an integer answer

Roger
27-01-2008, 20:08
I can never remember this, and my K&R is back at school.

int A;
long B;

int answerint;
long answerlong;

I think having a long in the expression casts it to long:

A * B produces a long. But to be sure you can cast it:

answerlong= (long) (A * B);

Alan Anderson
27-01-2008, 20:14
With a long as one of the operands, the Microchip C compiler will do a 32-bit multiplication. The result will be a long.