GATE CSE
First time here? Checkout the FAQ!
x
+1 vote
90 views

*var2++=*var3++;

How will this get evaluated?

In general will *var be evaluated first, or will var++ be evaluated?

The answer is A, I have run it on IDE but how is it evaluated?

asked in Programming by Loyal (3.8k points)  
retagged by | 90 views
how u can say int is 4 byte ???? it is compiler dependent.
I am not saying it. The question is implying it by default. Idk how to solve it.
Read operator precedence in C, only then you can attempt this question. Here confusing statement is *var2++=*var3++ --> here 3 operators are there i.e '=' , '++' , '*' --> read who has high precedence and solve it accordingly.
Wasn't able to find it on net. Is you have some link plz share

4 Answers

+3 votes
Best answer

Address of variables var2 and var3 are different.

after main() {

line no 3. var2 and var3, both pointing to same address, i,e &var1.

line no 4.  *var2++ = *var3++

*a++ can be written as:
*a;
a++;

here both * and ++ are unary operator precedence same but associaitvity right to left.

so (*(var2++)) = (*(var3++)), assignment has the least precedence among them (right to left), but increment is post, so variables' value (which is &var1) will increment after evaluation of expression.

It is just assigning 35 (*var3) to *var2 (which is var1), which was already 35, and they are incrementing var2 and var3 by 1×size_of_data_type

line no 5. var1++ ;  just increment 35 to 36.

Answer should be 36, var2, var3

= 36, &var1, &var1

Whatever the address of var1, it will be printed and both are equal.

answered by Loyal (4.3k points)  
selected by
I've ran the code on ide, address of var2 and 3 is same.
yes u are right. It is the address of var1, so it will be equal.
Addresses of var2 var3 are different but they both contain the same value i.e., the address of var1
0 votes
* and ++ have same precedence so to evaluate them you to go with associativity  
Associativity is     e.g:   if you same precedence operators on both sides of your variable like  +y+ or *ptr++ then
answered by Active (1.7k points)  
0 votes

* and ++ operators have same precedence so you have to go with associativity which is right to left for them

Associativity : if your operand is surrounded(right side and left side ) with same precedence operators like *ptr++  then to whom the ptr should be associated with is called associativity.

so in this case it is right to left and its evaluated as *(ptr++)  
ptr++ = address inside ptr + step size increment(integer size increment 4B or 2B)

Find all precedence and associativity here
https://www.programiz.com/c-programming/precedence-associativity-operators

answered by Active (1.7k points)  
0 votes

both * and ++ hav the same precendence 

http://www.difranco.net/compsci/C_Operator_Precedence_Table.htm

answered by Active (1k points)  


Top Users Sep 2017
  1. Habibkhan

    6826 Points

  2. Arjun

    2310 Points

  3. Warrior

    2302 Points

  4. nikunj

    1980 Points

  5. A_i_$_h

    1842 Points

  6. manu00x

    1750 Points

  7. Bikram

    1744 Points

  8. SiddharthMahapatra

    1718 Points

  9. makhdoom ghaya

    1690 Points

  10. rishu_darkshadow

    1672 Points


26,033 questions
33,610 answers
79,660 comments
31,066 users