Solved

How many will be the value of the expression (++x * ++x * ++x) for x = 5?

Posted on 2003-10-25
5
275 Views
Last Modified: 2010-04-15
i run the following programme under vc, gcc and tc:

#include <stdio.h>
#define CUBIC(x) ((x) * (x) * (x))

int main()
{
  int x = 5;
  int y = CUBIC(++x);
  printf("y=%d\n",y);
}

and get the results separately as follow:
y=392                    under vc and gcc
y=512                    under tc

why?
if y = CUBIC(x++), i get 125 under all the above compilers. why? i had thought the result will be depend on compiler and will be different.
0
Comment
Question by:bourbaki
5 Comments
 
LVL 9

Expert Comment

by:tinchos
ID: 9621748
I would have thought that the result would be 512, no idea of why is 392

I'll explain why I thought of 512

As you have a define, then the preprocessor would replace it before compiling it, so your code would be like this. (After preprocessing)

int main()
{
 int x = 5;
 int y = ( (++x) * (++x) * (++x) );             // Important Line
 printf("y=%d\n",y);
}

When it gets to "Important Line", it does the following

the first ++x                      so x = 6
then the second ++x         so x = 7
then the third ++x             so x = 8

then it evaluates y = 8 * 8 * 8;

then y = 512;

The reason for Cubic( x++ ) = 125 is that in that case

first y is assigned and afterwards x is incremented, so the y value is not influenced by the autoincrement operators

Hope this helps

Tincho
0
 
LVL 5

Expert Comment

by:dennis_george
ID: 9621757
hi bourbaki ,

   First i must tell you there is some difference in the expression evaluation in various compilers....

TC handles the expression differently and VC handles it differently.....

In TC environment if  one variable is occured many times in a expression then the final value of that vairable is substituted in all the places of its occurence......

The #define will replace  CUBIC(++x) to

y = (++x) * (++x) * (++x) ;

So in TC first the value(final valule) of x is  calcuated and then it is substituted in all its occurances....
so the final value of x after this expression will be 8

so y = 8 * 8 * 8 ;  = 512....


Now in VC the value  of x(or any variable) is done at the time of postfix evaluation....

NOTE:: In Computer expressions are normally evaluated by postfix evalutaion technique...


The postfix expression for (++x) * (++x) * (++x)

is   (++x)(++x)*(++x)*

so first (++x)(++x)* is calculated so the value of x is calculated here... here x is incremented twice so x = 7

(7)(7)* = 49....

Now the expression becomes (49)(++x) * and the value of x = 7;

Now again calculate (49)(++x)*   after increment x becomes 8
so (49)(8)* = 392...


So i hope u got some idea...

Dennis
 
0
 
LVL 15

Accepted Solution

by:
efn earned 50 total points
ID: 9621828
The question was why the answers were different in one case and the same in the other case.  The answer is simply that that's how the compilers were implemented.

With code like this, the language definition does not specify the order of the operations, so different compilers can do different things or the same thing without failing to conform to the standard.

So as a practical matter, it is better to avoid code like this than try to understand what it will do, because what it will do can legitimately vary from one compiler to another.

See also:

http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_20638918.html

--efn
0
 

Author Comment

by:bourbaki
ID: 9621856
hi Dennis,
   Thank you.
    Can the result be 6*7 when (++x)(++x)* is calculated? If x is increased and used its value,6,and then increased again and used its value, 7, and at last ...6*7.
    Can you tell me whether the analysis above is ture?

bourbaki
0
 
LVL 5

Expert Comment

by:dennis_george
ID: 9621880
Hi,

  No i don't think so...... when the compiler evaluate the value of a postfix expression, like (++x)(++x)*, it first calculates its value. Actually the postfix evaluation takes place in stack. both (++x) will be in stack. so before calculating the result it pops out the variables and calculates its value...so after two increments the value of x will be 7. so the compiler will calculate 7 * 7 not 6 * 7..... because during calculation of result it does not substitute the value after getting each variable but only after getting both the operands....


Dennis
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now