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
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Return int

    result will be 7 as it is of type int.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

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

    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
    576

    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 08: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,765

    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
  •  





Click Here to Expand Forum to Full Width

Featured