Samar and his thoughts

Wednesday, November 5, 2008

Prog:
main()
{
printf("%f",7/2);
}

7/2 is 3 right?? now 3 will be interpreted as float for printf. Now for each machine architecture the float representation is different.It is not known to us how the float value has been interpreted(at least not to me :-)).Hence any interger value when interpreted as float it is undefined what the value will be printed !!

Prog:
main()
{
int i=-1;
+i;
printf("%d%d",i,+i);
}

This program is quite silly one. Here (+) operator will act like only +2 i.e. to denote a +ive value. Hence when you do +i that means +(-1) which is as same as -1 only.Hence the output will be -1-1

Labels:

6 Comments:

  • This comment has been removed by the author.

    By Blogger Kushal Das, At November 7, 2008 4:46 AM  

  • int main(){
    int a = 7;
    int b = 2;
    printf("%f\n",a/b);
    return 0;
    }


    Output of this program is different from the output of your program on my machine.
    Any idea for this behavior??

    By Blogger Pratik, At November 11, 2008 12:45 AM  

  • @pratik:
    It is dependent on the architecture and compiler used.

    By Blogger Kushal Das, At November 11, 2008 12:53 AM  

  • Hi pratik,

    >> "Output of this program is different from the output of your program on my machine"

    I don't understand your comment. The explanation also holds good for your given program. By any chance if you are getting 0.0 or 0 as output then definitely the used compiler needs to be upgraded. I am using latest gcc 4.3.

    However try the following program
    main()
    {
    float a = 7/2;

    printf("%f\n",(a));
    }
    ... I hope, Your confusion is cleared now.

    By Blogger Samar, At November 12, 2008 12:50 AM  

  • hi Samar/kushal,
    It is not dependant on architecture/compiler as the 2 programs give different output on same machine and same compiler.
    I posted this question on a forum and following is what I got from there:
    ------------------------------------
    And exactly what output do you get from that - I'd expect that you'd get some pretty rubbish results, as you are printing an integer value as a floating point result. Since you are passing only an integer, half the result of your printout will depend on the content of where the other half of the double that the printf function expects. This is not at all clearly defined, and may be one of your integer values or some completely different random data - so yes, I expect that different bits of code would produce different results, but not in any way predictably - change the code around to printf("Hello, World\n") before the printf of the float and you'd get a different results. Add some calculations, change the compiler options, compile with a different compiler, etc, etc, and each time you change anything, it will change the results in some way [probably - there may be combinations that do not change the results - it is not easy to predict].
    -----------------------------------

    By Blogger Pratik, At November 13, 2008 4:15 AM  

  • @pratik,

    I don't think what we are trying to explain and what the forum ta1ks are different.May be we are taking about the subset of what was discussed in the forum.

    By Blogger Samar, At November 13, 2008 10:22 AM  

Post a Comment

Subscribe to Post Comments [Atom]



<< Home