Expression

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • amanda22
    New Member
    • Nov 2007
    • 8

    Expression

    Hi all,

    I have the following declaration and initialization:

    int n = 8;
    int z = 2.0;

    I know it would be 8, but I am trying to find out if the expression of z=n would be still considered an int, float, double, our unsigned.
  • RRick
    Recognized Expert Contributor
    • Feb 2007
    • 463

    #2
    z is what you define it to be, namely an int.

    When you assign 2.0 to z, the 2.0 float value will be convert to an integer and the integer value will be assigned to z.

    Any assignment between z and n will be as the types they are defined to be (and not the values you try to assign to them).

    Comment

    • amanda22
      New Member
      • Nov 2007
      • 8

      #3
      Thanks Rick, that's pretty much what I thought...it would stay an int. I guess the float just got me thinking.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Originally posted by amanda22
        int z = 2.0;
        will cause a compiler warning about truncation and possible loss of data. 2.0 is a double and z is an integer. Integers do not have decimal places plus the double can hold a value larger than the int can contain.

        C and C++ have two data variable familes: integer and floating point.

        Try to not mix these in your code.

        Comment

        Working...