# Return int

• February 13th, 2014, 01:17 PM
andraesatchell
Return int
int result = 15/2;
is it that result will now be 7 or 7.5. based on my understanding it will be 7. since it is returning an integer. am I correct to say that the .5 will be discarded.
• February 13th, 2014, 01:24 PM
2kaud
Re: Return int
result will be 7 as it is of type int.
• February 13th, 2014, 01:45 PM
GCDEF
Re: Return int
That would be easy enough to test wouldn't it?
• February 16th, 2014, 04:52 PM
Jose M
Re: Return int
Yes, you are right.
What you are doing is an implicit cast wich is a conversion type from float to int. Your are casting the type float (or double) to int.
• February 16th, 2014, 06:46 PM
razzle
Re: Return int
Quote:

Originally Posted by Jose M
Yes, you are right.
What you are doing is an implicit cast wich is a conversion type from float to int. Your are casting the type float (or double) to int.

No.

Integer division of integers will produce an integer (according to the rules of integer division).

There's no implicit conversion taking place at all.
• February 17th, 2014, 01:22 AM
Jose M
Re: Return int
Yes it's true. I didn't know it.

Then the cast would be this:

int result=15.0f/2;

Obviously here there's cast ...true????????
because the result of the division would be 7.5 but finally the value of the variable 'result' will be 7.

Do you agree???
• February 17th, 2014, 01:59 AM
parikshit6321
Re: Return int
Actually casting is done by adding the data type in paranthesis before the variable you want to type cast such as (float)var.

What you are doing (15.0f/2) is again a float but according to the division rules which say float and integer division will always be float.
• February 17th, 2014, 02:09 AM
laserlight
Re: Return int
Quote:

Originally Posted by Jose M
Then the cast would be this:

int result=15.0f/2;

Obviously here there's cast ...true????????

We would normally say conversion rather than "cast" since it is implicit rather than explicit. Note that a compiler might complain about possible loss of data in such a conversion, so an explicit cast might be used anyway.