Thread: Confused with the Output.....

  1. #1
    Registered User
    Join Date
    Dec 2010
    Location
    Lucknow, India
    Posts
    72

    Confused with the Output.....

    Hi Guys. I have two programs with an Output.. Need Explanation on the Output..!!

    Program 1:
    Code:
    void main()
    {
    printf("%u",main);
    }
    This Program print some garbage Value. But I want to know how..??? As I dont declare anything like "main" variable.

    Program 2:
    Code:
    void main()
    {
    float f=1.1;
    double d=1.1;
    if(f==d)
     printf("I Love Java");
    else
     printf("I Love C");
    }
    here the Output is- I Love C. here I wanna know how this Conversion is made in the memory that they are't equal..??
    please Reply.
    There is a Real magic in enthusiasm, It spells the difference between mediocrity and accomplishments.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Gaurav Singh View Post
    Hi Guys. I have two programs with an Output.. Need Explanation on the Output..!!

    Program 1:
    Code:
    void main()
    {
    printf("%u",main);
    }
    This Program print some garbage Value. But I want to know how..??? As I dont declare anything like "main" variable.
    Most likely it's printing the address of main().


    Program 2:
    Code:
    void main()
    {
    float f=1.1;
    double d=1.1;
    if(f==d)
     printf("I Love Java");
    else
     printf("I Love C");
    }
    here the Output is- I Love C. here I wanna know how this Conversion is made in the memory that they are't equal..??
    please Reply.
    You are comparing dissimilar types... try it as... if (d == (double) f) ... and see what happens.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by CommonTater
    You are comparing dissimilar types... try it as... if (d == (double) f) ... and see what happens.
    Even then, because of floating point inaccuracy, you may find that the comparison still evaluates to false.
    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

  4. #4
    Registered User
    Join Date
    Dec 2010
    Location
    Lucknow, India
    Posts
    72
    You are comparing dissimilar types... try it as... if (d == (double) f) ... and see what happens.
    Tried.. No change in the Output.
    There is a Real magic in enthusiasm, It spells the difference between mediocrity and accomplishments.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Gaurav Singh
    Tried.. No change in the Output.
    The moral of the story: be careful when you want to compare floating point values for equality, as what appears to be equal to you may not actually be equal, even if they are of the same type.
    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

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    He could try this...
    Code:
    if (abs(d - (double) f) < 0.005)
    Depending on allowable margin of error he should be able to adjust this to work...
    Last edited by CommonTater; 10-12-2011 at 12:05 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ overlapping output and adding extensions to output files
    By lordmorgul in forum Linux Programming
    Replies: 9
    Last Post: 05-11-2010, 08:26 AM
  2. How to edit output in struct and call for the output
    By andrewkho in forum C Programming
    Replies: 4
    Last Post: 03-16-2010, 10:28 PM
  3. output of struct? confused....
    By Soulzityr in forum C Programming
    Replies: 11
    Last Post: 03-01-2010, 03:54 PM
  4. terminal output not showing output properly
    By stanlvw in forum C Programming
    Replies: 13
    Last Post: 11-19-2007, 10:46 PM
  5. Replies: 3
    Last Post: 02-19-2003, 08:34 PM