On Tue, 18 Jan 2000, Aseem Rane wrote:

> 
> if i is initialized to say 3,
> 
> what will be the value of j, where
> 
> j = ++i * ++i * ++i;
> 
> on solaris 
> using cc answer is 
> j = 6 * 6 * 6;
> 
> useing gcc answer is
> j = 4 * 5 * 6;
> 

> but on linux (RH6.1 PCQ)
> 
> j = 5 * 5 * 6;
> 
> can anybody explain logic behind j's value on linux???
> 
> bye
> aseem
> 
> 

Dear Aseem,
        Please quote the version of gcc you where using and the platforms
individually. As far as I understand you, you have tested on these lpatforms :
Solaris  and   cc
Solaric and    gcc
Linux    and    gcc

If I have NOT understood correctly, the following does NOT hold...
        It is rather strange that gcc would be giving different results on
Linux and Solaris..  Are you sure you are not typing something different in
Linux and Solaris like brackets and stuff  ?? If not, the the following hold :

        a) There could be major changes in the parsing and/or symbol table
representation method used in gcc (say version x, which you have run under
solaris ) and gcc (Version y, which you have run under Linux)

        b)  Point (a) is highly improbable.

        Of all the extensive experiments I have conducted under gcc (egcs
-2.91.66) / Linux, I have got completely explicable and expected results. The
parsing algo makes perfect sense and the reason why the answer is 150 is VERY
clear. I would have loved to explain it to the people who are losing there
sleep over this issue but for the fact that the explanation needs a whole lot
of explanation of  
        o Postfix evaluations
        o Operator precedence parsing
        o Operator associativity
        o Parse tree Evaluation
        o Symbol table representation
        o Junior School Arithmetic

        It is not possible to explain this in such a short span of time and
space and email is the worst medium for such explanations requiring high
involvement of interactive participation and graphical visualisation. (This is
the closest I could get to Fermat's Last Theorem :) Alas, only if we had an
electronic whiteboard / videoconf'ing .

        And to top it all, it is 00:53 hrs and I am running short on Kaffeine.

IF YOU HAVE COME TILL HERE WITHOUT HITTING THE DELETE BUTTON IN DISGUST,
let me apologise for being forcibly funny. Certain observations are in order :

        a) Don't ever think of coding your programs in this way. I am sure that you
don't, and this was some kind of an experimental exercise to you for the fun of
hacking and stuff. 

        b)The C language specification does not provide extensive
support for such cases and the output is implementation dependent, ie your
Compiler is free to do whatever it feels like. In the K&R white book (now blue),
you are explicity warned against such adventures. 


        c) I *am* serious about the existence of a logical algorithm but it is
really *tough* for me to explain it if we are not face to face. I think I
should read the HOWTO- HOWTO

        d) When I started exploring this problem, I too was perplexed like you
and had to take resort to K&R for some clarifications on Op precedence and
associativity. This issue is *non-trivial* and initially I was foxed, too.
However, I could reverse engineer my way back to the correct solution.

        e) Somebuddy wrote :
               "        i tried on linux ver 4.2
                j=4*5*6 was the answer i got.
                also logically it is the correct answer.
               "
            To that anotherbuddy replied :
               "         
                Shouldn't the 'logically correct' answer have been
                j=6*5*4 rather than 4*5*6 ?
               "        
             Buddies, it beats me as to why either of these should be
"logically" correct. The first is close to what we do in real life maths but
doesn't pay any attention to mathematical logic, i mean the rules of operator
precedence , think BODMAS. (Only that in BODMAS, we dont have the ++ op)
The second one,  probably wanted to draw an analogy between some kind of
stack operation and "6*5*4", but hey buddy thats how parameter passing in
functions happen not parsing (in this case). Stacks are used in postfix
evaluation but not in the order which you think to be logical. In fact ,
the postfix simulation of this expression is real jittery.. yeah i had a tough
time doing it by hand :)

        e) Thanks Pramod for reminding us the First Commandment of C
            programming "Thou shall use Lint". Folks should _REALLY_ get the
            lint dnload from the link provided. 

        f) Suvendra , you snatched the words from my mouth about the
interviewers. Thanks, you couldn't have been more correct. You have done 
agreat job in explaining as far as possible the hows, do's and dont-s behind 
the problem. But your explanation of case 2 :
        
                Case 2: (++i)*(++i)*(++i) 4*5*6

        will not compile to give 120 under Linux/gcc (I used version 2.91). It
will give you 150.


        g) Was expecting something from Binand on this, fundu self-printing
            proggie and also from Shanker (csewhiz@flashmail), probably busy
            with JServ

        h) Finally, I am extremely sorry for puking so much bad funda
        about parsing. Just finished my sems having Compiler Theory. Please forgive me,
        O Gurus for this vanity. (To be frank, I know *alls about this stuff )


Warmest regards
Shourya


--
_______________________________________________________________
Shourya Sarcar         <[EMAIL PROTECTED]>  <Tel:91-033-4710477>
Department of Computer Science and Engineering
Jadavpur University    Calcutta, India 700 032

All the world's a stage..
And I am acting tonight
C - the difference : http://www.eskimo.com/~scs/C-faq/top.html

--------------------------------------------------------------------
The Linux India Mailing List Archives are now available.  Please search
the archive at http://lists.linux-india.org/ before posting your question
to avoid repetition and save bandwidth.

Reply via email to