0
#define CUBE(x)(x*x*x)
int main()
{
    int a, b;
    b = 3;
    a = CUBE(b++)/b++;
    printf(a=%d b=%d\n", a, b);
    return 0;
}

I have a confusion in this Macro defining statement? and I need output too?

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
Arunjunai
  • 11
  • 2
  • what is the confusion ? – Satya Sep 14 '13 at 05:17
  • Routine old undefined behaviour...what's the canonical question that this is a duplicate of? You can't modify `b` four times between sequence points and expect to get anything sane. Strictly, you can't invoke `printf()` without a prototype in scope either – you don't show the `#include ` line. However, that usually works. The `(b++*b++*b++)/b++` nonsense has no answer that you can expect — you can find what a specific compiler does, but it's under no obligation to do anything sane. – Jonathan Leffler Sep 14 '13 at 05:20

3 Answers3

1

The part

CUBE(b++)

will be converted to

(b++ * b++ * b++)

which is undefined behavior or problematic due to modifying a variable in one single statement. It's recommended to avoid such a thing.

Try to pass a variable without ++ or --.

masoud
  • 55,379
  • 16
  • 141
  • 208
0
a=CUBE(b++)/b++;  

      |  
      V  


a=(b++ * b++ * b++)/b++ ;    

In the above expression b value modifies between sequence points would cause undefined behavior due to lack of Sequence_point

To avoid this first assign and then increment

   a=CUBE(b)/b;
   b++; 
Gangadhar
  • 10,248
  • 3
  • 31
  • 50
0
  1. Use #define CUBE(x) ((x)*(x)*(x)) to avoid mistake when x is an expression.

  2. a = CUBE(b++)/(b++); The value of a after executing the statement depends on the compiler you use or something else. This is called undefined behavior.