Thread: Can't understand this !!

  1. #1
    Registered User
    Join Date
    Nov 2017
    Posts
    34

    Unhappy Can't understand this !!

    Code:
    #include <stdio.h>
    
    void main()
    {
       printf("%d",59.00); //prints out 2147483619
    }

    I can't understand the output of this program! Please help!

  2. #2
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338
    Code:
      printf("%f",59.00);
    "without goto we would be wtf'd"

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Do you want to know WHY 2147483619 was printed or the answer @Structure gave you is enough?

  4. #4
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Quote Originally Posted by ranadas View Post
    Code:
    #include <stdio.h>
    
    void main()
    {
       printf("%d",59.00); //prints out 2147483619
    }

    I can't understand the output of this program! Please help!
    A quick google on printf woulda saved you the time you spent on posting this.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well since it began with "void main", printing "I'm a banana" would have been no less undefined than any other answer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Quote Originally Posted by Salem View Post
    Well since it began with "void main", printing "I'm a banana" would have been no less undefined than any other answer.
    Damn I'm slow to notice that XD

  7. #7
    Registered User
    Join Date
    Nov 2017
    Posts
    34
    Why was it wrong on my part to use "void main"? And can you please tell me why the result came out to be 2146483619 , i still don't understand even after reading answers on stack overflow.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ranadas
    Why was it wrong on my part to use "void main"?
    It's wrong by definition: the return type of main is int.

    Quote Originally Posted by ranadas
    And can you please tell me why the result came out to be 2146483619
    The format specifier you used is %d, which expects a corresponding int argument. You passed 59.00 as the corresponding argument, which is a double. Since the format specifier and the corresponding argument do not match, the behaviour is undefined. So, don't do this. Your compiler will typically be able to warn you about such mistakes.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Nov 2017
    Posts
    34
    Then why only print 2147483619? it could have printed 728 or 753 or 283728983 something like this.. there must be some logic , it is not garbage value

  10. #10
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    Quote Originally Posted by ranadas View Post
    Then why only print 2147483619? it could have printed 728 or 753 or 283728983 something like this.. there must be some logic , it is not garbage value
    Although there is some "logic" to it, it is not necessarily interesting. It is basically a garbage value. The exact value depends on your compiler/CPU/OS. For example, when I ran your original program I got the value -914973672.

    One possibility is that it is interpretting the first half of the binary representation of the 64-bit double floating point representation as a 32-bit integer. But that is not necessarily what is happening, since it's possible that ints and floating point values are passed in different ways. They are not always passed on the stack in modern systems. For example, look at my results for the following:
    Code:
    #include <stdio.h>
     
    int main()
    {
       printf("%d\n", 123);   // prints 123
       printf("%d\n", 59.00); // prints 123
       return 0;
    }
    Last edited by john.c; 01-28-2020 at 11:01 PM.
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help.. I can't understand
    By cacca in forum C Programming
    Replies: 7
    Last Post: 06-19-2012, 04:22 AM
  2. please help me understand this
    By litzkrieg in forum C Programming
    Replies: 8
    Last Post: 02-16-2011, 01:44 PM
  3. Can someone help me understand this plz?
    By Arex Bawrin in forum C Programming
    Replies: 5
    Last Post: 02-18-2010, 06:42 PM
  4. I don't understand this
    By Dontgiveup in forum C++ Programming
    Replies: 14
    Last Post: 03-28-2009, 07:04 PM
  5. help me understand...
    By stormfront in forum C Programming
    Replies: 2
    Last Post: 10-12-2005, 10:47 AM

Tags for this Thread