Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. what is the value of y after the following statements?

what is the value of y after the following statements?

Scheduled Pinned Locked Moved C / C++ / MFC
question
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 13315055
    wrote on last edited by
    #1

    what is the value of y after the following statements? float y; y = 17 / 5;

    Richard DeemingR _ 2 Replies Last reply
    0
    • U User 13315055

      what is the value of y after the following statements? float y; y = 17 / 5;

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      Probably 3, because you're dividing two integers and then storing the resulting integer in a float. But why don't you run it for yourself and find out?


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      1 Reply Last reply
      0
      • U User 13315055

        what is the value of y after the following statements? float y; y = 17 / 5;

        _ Offline
        _ Offline
        _Superman_
        wrote on last edited by
        #3

        As Richard said, the expression only contains integers and so you should get the result of an integer division (3). However, if at least one of the operands / variables are of float type, all integers will be automatically converted to float type. Say, for example - y = (float)17 / 5; Having said this, the answer may be slightly different from the expected 3.4 because of how floating point numbers are represented. Please check this - Floating point inaccuracy examples - Stack Overflow[^]

        «_Superman_»  _I love work. It gives me something to do between weekends.

        _Microsoft MVP (Visual C++) (October 2009 - September 2013)

        Polymorphism in C

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups