casting

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • eslam abotaleb
    New Member
    • Feb 2014
    • 2

    casting

    i want convert int to char
  • stdq
    New Member
    • Apr 2013
    • 94

    #2
    Hi! Can you post your code? Thanks!

    Comment

    • eslam abotaleb
      New Member
      • Feb 2014
      • 2

      #3
      Write a program that reads in three floating-point numbers and sorts them.
      For example
      4
      9
      2.5
      Use only conditional statement
      2.5
      4
      9
      this example want convert int to float or not

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Be careful here.

        You cannot convert an int to a char. An int can be 32 or even 64 bits while a char is typically 8 bits. You cannot stuff 32 bits into 8. The compiler gives a warning here about possible loss of data. You can do a cast but all that does is shut off the warning. 32 bits still cannot be packed into 8.

        BTW: is was just this situation in the guidance software of a French Ariadne satellite missile that caused it to dump into the ocean for a multi-million dollar loss.

        You can convert little things into big things. float to double is OK but double to float is not.

        char to short is OK. short to int is OK. int to long is OK. But not the other way around because of possible data loss.

        int to float is bad news. Only 6 significant figures in a float leading to loss of data again.

        float to int is never OK. double to int is never OK. All the decimals get chopped off leading to loss of data. The compiler warns about this and you have to do cast to shut the warning off.

        float to double is OK but double to float is not. double is big while float is small. Loss of data again.

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          Why would you want to "convert int to float" in that sorting example?

          Are you asking how to convert a string ("45", "-34.92", etc) into a number?

          Comment

          • Nepomuk
            Recognized Expert Specialist
            • Aug 2007
            • 3111

            #6
            Could it be, that you're trying to create a string from an integer value by any chance? (So something like int value = 42 to char* answer = "42"?) If so, you can safely convert ints to chars digit by digit and then save that to a string.

            @weaknessforcat s: You are bringing out the big guns with that reply, aren't you? :-D

            Comment

            Working...