Return int
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Return int

  1. #1
    Join Date
    Feb 2014
    Posts
    6

    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.

  2. #2
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,455

    Re: Return int

    result will be 7 as it is of type int.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,107

    Re: Return int

    That would be easy enough to test wouldn't it?

  4. #4
    Join Date
    Feb 2014
    Posts
    11

    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.

  5. #5
    Join Date
    Jul 2013
    Posts
    288

    Re: Return int

    Quote Originally Posted by Jose M View Post
    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.
    Last edited by razzle; February 16th, 2014 at 07:08 PM.

  6. #6
    Join Date
    Feb 2014
    Posts
    11

    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???

  7. #7
    Join Date
    Feb 2014
    Location
    India
    Posts
    5

    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.

  8. #8
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,294

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center