How is output 0?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jyoti300892
    New Member
    • Jun 2014
    • 1

    How is output 0?

    #include<stdio. h>
    #include<math.h >
    #include<conio. h>

    void main()
    {
    printf("%d",(36 .0));
    getch();
    }
    how output is 0
  • Luuk
    Recognized Expert Top Contributor
    • Mar 2012
    • 1043

    #2
    no, it does not output '0', see below:

    Code:
    luuk@opensuse:~/tmp> cat test.c
    #include<stdio.h>
    #include<math.h>
    
    void main()
    {
            printf("%d\n",(36.0));
    }
    luuk@opensuse:~/tmp> make test
    cc     test.c   -o test
    luuk@opensuse:~/tmp> ./test
    -733485528
    luuk@opensuse:~/tmp> ./test
    623260056
    luuk@opensuse:~/tmp> ./test
    694159224
    luuk@opensuse:~/tmp> ./test
    -1080378728
    luuk@opensuse:~/tmp> ./test
    2002711112
    luuk@opensuse:~/tmp>

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      The "%d" argument to printf tells it to decode the next argument as an int. However, you passed a double. It should come as no surprise that the output is garbled.

      Comment

      • mHealth
        New Member
        • Jun 2014
        • 13

        #4
        Originally posted by Luuk
        no, it does not output '0', see below:

        Code:
        luuk@opensuse:~/tmp> cat test.c
        #include<stdio.h>
        #include<math.h>
        
        void main()
        {
                printf("%d\n",(36.0));
        }
        luuk@opensuse:~/tmp> make test
        cc     test.c   -o test
        luuk@opensuse:~/tmp> ./test
        -733485528
        luuk@opensuse:~/tmp> ./test
        623260056
        luuk@opensuse:~/tmp> ./test
        694159224
        luuk@opensuse:~/tmp> ./test
        -1080378728
        luuk@opensuse:~/tmp> ./test
        2002711112
        luuk@opensuse:~/tmp>
        Any idea, why is the output varying? Should it take
        first 4 bytes and print?

        Comment

        • Luuk
          Recognized Expert Top Contributor
          • Mar 2012
          • 1043

          #5
          no idea why it's varying, but you should use this:
          Code:
          printf("%d",(int)(36.0));
          or
          Code:
          printf("%4.0f",(36.0));

          Comment

          • meditation
            New Member
            • Jun 2014
            • 13

            #6
            Because it is reading random memory location.

            Comment

            • donbock
              Recognized Expert Top Contributor
              • Mar 2008
              • 2427

              #7
              It is not reading random memory locations.
              The displayed value varies because different compiler implementations use different encoding schemes for floating point numbers. You should never write code that takes advantage of implementation-specific characteristics such as floating point encoding scheme.

              Comment

              • aswal
                New Member
                • Aug 2013
                • 38

                #8
                @donbock

                What does "encoding schemes for floating point numbers" means?

                Comment

                • Luuk
                  Recognized Expert Top Contributor
                  • Mar 2012
                  • 1043

                  #9
                  In simple English, it's the way how floating point numbers are stored in memory.

                  A human will 'store' the value of 2/10 as 'zero dot two' (or something)

                  A computer, or compiler, might do that differently.

                  Comment

                  • donbock
                    Recognized Expert Top Contributor
                    • Mar 2008
                    • 2427

                    #10
                    In general, computer memory only contains integer numbers. However, we want the computer to handle other kinds of things (such as floating point number and strings). This is accomplished by encoding these other things as integer numbers (or sequences of integer numbers).

                    The example you are probably most familiar with is the ASCII encoding for representing printable characters as integer numbers (65 means 'A', 66 means 'B', etc). However, this is not the only way to encode characters (see EBCDIC).

                    Comment

                    • aswal
                      New Member
                      • Aug 2013
                      • 38

                      #11
                      @donbock

                      can you please list me the sources where i can learn more about this topic.

                      Comment

                      • Luuk
                        Recognized Expert Top Contributor
                        • Mar 2012
                        • 1043

                        #12
                        @donbock:
                        "In general, computer memory only contains integer numbers"

                        In general, computer memory only contains binary (zero's and/or one's)
                        Claiming that a computer stores integer numbers is a simplification.

                        When a computer stored the next sequence of bits:
                        01000001
                        A human wants to read this as an integer, and starts calculation:
                        1*2^0+0*2^1+0*2 ^2+...+1*2^6 = 65
                        But if this person was counting in hexadecimal he would say:
                        41
                        (4*16+1=65; but also written in binary: '0100 0001')

                        Comment

                        • donbock
                          Recognized Expert Top Contributor
                          • Mar 2008
                          • 2427

                          #13
                          @Luuk: yes, I was deliberately simplifying.
                          By the way, 01000001 (binary), 65 (decimal), and 41 (hexadecimal) are all integers. It doesn't matter that they use different bases.

                          @aswal: you can start by looking up character encoding and IEEE Floating Point in Wikipedia.

                          Comment

                          • Luuk
                            Recognized Expert Top Contributor
                            • Mar 2012
                            • 1043

                            #14
                            @donbock: NO, 41 is not an integer (in this context)

                            It is a hexadecimal representation of the integer 65.

                            Comment

                            Working...