Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

plz expain the underlined snippet of c

+2 votes
asked Feb 15, 2021 by Ushnika (250 points)

#include <stdio.h>
int main()
{
    int age=45;
    printf("%d\n",age-- + --age);
                 //(45+43)=88
    printf("%d\t",age);  //age=43
   printf("%d\n",--age + --age); //42+41=83

   printf("%d\t",age);  //age=41
   printf("%d\n",++age + ++age); //42+43


                 
    return 0;
}

1 Answer

0 votes
answered Feb 17, 2021 by Peter Minarik (84,720 points)
edited Feb 18, 2021 by Peter Minarik

What we see in this little program is demonstration of the pre- and post-increments (and pre-/post-decrement) operators.

Pre-increment: ++a

It increments the value of a, then whatever expression this value is used will have the new value.

With pseudo code:

Type preincrement(Type value)
{
    increment(value);
    return value;
}

Post-increment: a++

It increments the value of a, but only after its original value was used in the expression.

With pseudo code:

Type postincrement(Type value)
{
    Type tmp = value;
    increment(value);
    return tmp;
}

Example #1

When age = 45,

printf("%d\n", age-- + --age);
  1. first uses the value of age (45) in the addition and decrements its value (45 - 1 = 44) and the other part of the addition is
  2. the decremented value of the "new" age (44 - 1 = 43)

Example #2

When age = 43,

printf("%d\n", --age + --age);
  1. first decreases the value of age (43 - 1 = 42) then adds this to
  2. the decreased value of the "new" age (42 - 1 = 41).

Example #3

When age = 41,

printf("%d\n", ++age + ++age);
  1. first increments the value of age (41 + 1 = 42) and adds this to
  2. the incremented value of the "new" age (42 + 1 = 43)

I hope this help. :)

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...