retagged by
835 views
1 votes
1 votes

What will be the output$?$

int main()
{
  int varl = 35,*var2,*var3;
  var2 = &var1; //suppose the address of var1 is 1006
  var3 = var2;
  *var2++ = *var3++;
  var1++;
  printf("var1 = %d var2 = %d var3 = %d ",var1,var2,var3);
  return 0;
}
  1. 36  1010  1010
  2. 38  1006  1006
  3. 37  1006  1010
  4. 38  1010  1006
retagged by

4 Answers

Best answer
4 votes
4 votes

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.

selected by
0 votes
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
0 votes
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

Related questions

1 votes
1 votes
1 answer
1
1 votes
1 votes
2 answers
2
0 votes
0 votes
1 answer
3
sushmita asked Mar 27, 2017
512 views
Find the out put of the following C program. main() { char *ptr = "techtud"; char p= (*ptr)++; printf("%s\n",ptr); }the output of the program came as$?$