printf errors

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KrisKris
    New Member
    • Oct 2011
    • 5

    #1

    printf errors

    Hi there,
    I have to write simple programm and test what heppens when I incorrectly identify types for output.

    for example:
    main(){
    int a=5;
    printf("value is %f",a);
    }
    outcome of the print statement is 0.000000 instead of 5. why?

    - if use %d instead of %f
    outcome of the print statement is -1610612736 instead of 5.567, why?

    - if use %c instead of %d
    outcome of the print statement is (sign) instead of 5. why?

    -if use %d instead of %c
    outcome of the print statement is 97 instead of "a". why?

    - if use % instead of %s
    outcome of the print statement is 2686746 instead of "Sean". why?

    - if use %s instead of %d
    the program stop working.

    I know all types have a different lenght and location but would to know why the computer interpretation is wrong.

    Thank you very much for help
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    It looks like you need to read up on printf format strings.

    Basically if you supply 1 data type but tell printf it is another data type then the data, whatever it might be will be interpreted as that type.

    For example you supply the integer 5 and ask printf to interpret it as a float. The binary representation of int 5 is

    0000 0000 0000 0000 0000 0000 0000 0101

    When this binary representation is interpreted as a float then the float has a value of 0.0

    The actual binary representation of 5.0f is

    0100 0000 1010 0000 0000 0000 0000 0000

    The bits are very different as you can see.

    A similar thing happens for all the other examples you give. You supply data in 1 format and ask for it to be interpreted as another format without converting the data so the interpretation gives a result that you aren't expecting.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      What is the meaning of this binary value?
      0110 0001 0100 1101 0101 1010 0100 1100
      • interpreted as a hexadecimal number, it is 0x614D5A4C
      • interpreted as an unsigned 32-bit integer, it is 1,632,459,340
      • interpreted as unsigned 16-bit integers, it is 24,909 and 23,116
      • interpreted as ASCII characters is it aMZL
      • interpreted as EBCDIC characters it is /(!<
      • interpreted as floating point it is ... something else
      • interpreted as an instruction it is ... something else (depending on which processor it is for)
      • ...
      • and so on


      Don't think of that binary number as a value, think of it as a code. What it truly means depends on which encoding system you use.

      In your printf experiments you create a binary number based on one encoding system (int) and then interpret that value based on other encoding systems (as per the format specifier). You shouldn't be surprised that the meaning varies.

      What do you think of when I say "Nova"?
      It might be a new star;
      It might be an old car;
      In Spanish, it means "it does not go".

      Comment

      • KrisKris
        New Member
        • Oct 2011
        • 5

        #4
        Thank you guys very much.
        It will help.

        I am waiting for other explanation, also.
        Thanks
        Kris

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          What else do you need an explanation for?

          Comment

          Working...