Find the answer to your Linux question:
Results 1 to 3 of 3
Can any1 plz tell me how increment & and decrement operators work in C as well as in Linux if known...! As far as I knwo there are post & ...
  1. #1
    Just Joined! ajay2nokia's Avatar
    Join Date
    Jan 2008
    Location
    PUNE
    Posts
    4

    Question How Does Increment/Decrement Operators Work...??

    Can any1 plz tell me how increment & and decrement operators work in C as well as in Linux if known...!
    As far as I knwo there are post & pre increment/decrement operators :
    I m getting diff results when calculated manually & different when programmed in C ....!!
    Suppose for ex :

    a = 2b + (- - b) + 3(b++) +b

    if b = 3
    what is ans of b
    manually ans is = 6 + 2 + (3*2) + 3 = 17
    but when programmed ans is 19 yyyyyyy..........???
    nt only in this case in many cases it happens i want to know the priorities which comes 1st and all ThX in adv....!!
    Last edited by techieMoe; 02-06-2008 at 03:04 PM. Reason: Lower-cased the first paragraph... Please don't shout.

  2. #2
    Just Joined!
    Join Date
    Jan 2008
    Posts
    25
    First of all, its a very bad idea to write a single experssion in which one operand depends on the value of another operand.
    In general, the expressions are evaluated according to the precedence of the operators. In your case, the b++ operand should be evaluated first then --b followed by * and + respectively. Then one has to worry about associativity. But, there is usually no guarantee in which order the operands are evaluated.
    As far as I can tell what is happening in your example is that first the b++ term is evaluated then 2*b follwed by --b and then +b. So, this is what you would get
    2*3 + 2 + 3*3 + 2 = 19 (I assume this is not the answer you were looking for but you were looking for 17)
    That is why you will have to be very careful when writing a single experssion where one operand depends on the value of another operand. In fact, I must again say that one should avoid using such experssions.

  3. #3
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    What mdnayeem said.

    As an illustration, I tried running this program, which evaluates the terms of your expression in all 24 possible orders:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      int a;
      int b;
    
      b=3;
    
      a  = 0; a += 2*b;     a += (--b);   a += 3*(b++); a += b;       printf("%d ",a);
      a  = 0; a += 2*b;     a += (--b);   a += b;       a += 3*(b++); printf("%d ",a);
      a  = 0; a += 2*b;     a += 3*(b++); a += (--b);   a += b;       printf("%d ",a);
      a  = 0; a += 2*b;     a += b;       a += (--b);   a += 3*(b++); printf("%d ",a);
      a  = 0; a += 2*b;     a += 3*(b++); a += b;       a += (--b);   printf("%d ",a);
      a  = 0; a += 2*b;     a += b;       a += 3*(b++); a += (--b);   printf("%d ",a);
      a  = 0; a += (--b);   a += 2*b;     a += 3*(b++); a += b;       printf("%d ",a);
      a  = 0; a += (--b);   a += 2*b;     a += b;       a += 3*(b++); printf("%d ",a);
      a  = 0; a += 3*(b++); a += 2*b;     a += (--b);   a += b;       printf("%d ",a);
      a  = 0; a += b;       a += 2*b;     a += (--b);   a += 3*(b++); printf("%d ",a);
      a  = 0; a += 3*(b++); a += 2*b;     a += b;       a += (--b);   printf("%d ",a);
      a  = 0; a += b;       a += 2*b;     a += 3*(b++); a += (--b);   printf("%d ",a);
      a  = 0; a += (--b);   a += 3*(b++); a += 2*b;     a += b;       printf("%d ",a);
      a  = 0; a += (--b);   a += b;       a += 2*b;     a += 3*(b++); printf("%d ",a);
      a  = 0; a += 3*(b++); a += (--b);   a += 2*b;     a += b;       printf("%d ",a);
      a  = 0; a += b;       a += (--b);   a += 2*b;     a += 3*(b++); printf("%d ",a);
      a  = 0; a += 3*(b++); a += b;       a += 2*b;     a += (--b);   printf("%d ",a);
      a  = 0; a += b;       a += 3*(b++); a += 2*b;     a += (--b);   printf("%d ",a);
      a  = 0; a += (--b);   a += 3*(b++); a += b;       a += 2*b;     printf("%d ",a);
      a  = 0; a += (--b);   a += b;       a += 3*(b++); a += 2*b;     printf("%d ",a);
      a  = 0; a += 3*(b++); a += (--b);   a += b;       a += 2*b;     printf("%d ",a);
      a  = 0; a += b;       a += (--b);   a += 3*(b++); a += 2*b;     printf("%d ",a);
      a  = 0; a += 3*(b++); a += b;       a += (--b);   a += 2*b;     printf("%d ",a);
      a  = 0; a += b;       a += 3*(b++); a += (--b);   a += 2*b;     printf("%d ",a);
    
      printf("\n");
    
      return 0;
    
    } /* main() */
    The output:
    Code:
    17 16 21 17 22 21 15 14 23 17 24 21 17 14 21 15 24 23 17 16 21 17 22 21
    --
    Bill

    Old age and treachery will overcome youth and skill.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...