C Programming - Pointers - Discussion

Discussion Forum : Pointers - Find Output of Program (Q.No. 2)
2.
What will be the output of the program ?
#include<stdio.h>

int main()
{
    int i=3, *j, k;
    j = &i;
    printf("%d\n", i**j*i+*j);
    return 0;
}
30
27
9
3
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
65 comments Page 4 of 7.

Chaitu said:   1 decade ago
It first calculate the value of (i**j*i).
That is (i=3, *j=* (&i) =3, i=3) =3*3*3=27.
Then it add *j=* (&i) =3.
So total equation is= ( (3*3*3) +3) =30.

Satti(iiit) said:   1 decade ago
Very nice munny. I am getting your explanation.

Rajesh Gautham said:   1 decade ago
It is stored in order of (*j) , +, (i) , *, (*j) , *, (i).
Here i=3, *j also 3 according to the precedence order it will execute like this.
(3) * (3) * (3) + (3) =30.

Srinivas said:   1 decade ago
#include<stdio.h>

int main()
{
int i=3, *j, k;
j = &i;
printf("%d\n", i**j*i+*j);
return 0;
}
In this program,we are storing the address of the value 3 in j.And *j is pointing to the same value 3.In the printing statement ,it will take *j=3,and i=3.so (i)*(*j)*i+*j
3*3*3+3=30

Tanu said:   1 decade ago
Arrange the expression as : (i*(*j)*i)+(*j))
Now, i = 3
*j = (Value at Address of) j = 3

So, expression comes out to be: (3*3*3)+(3)) = 30.

Vivek.A said:   1 decade ago
*(dereference operator) has higher precedence than the arithmetic operators. So during the evaluation of the expression
i**j*i+*j
The *j components are first evaluated and placed at their respective positions
(i.e) i*3*i+3
Then as usual arithmetics are performed to get 30

Sai said:   1 decade ago
3*3*3+3=30

Vinay prasad said:   1 decade ago
j is a pointer
hence,j=&i (ie,*j=3) becuase i=3
i**j=3*3=9
i**j*i=9*3=27
i**j*i+*j=27+3=30

Bhardwaj said:   1 decade ago
j is a pointer and that hold an address of the variable.
So j holds an address of i i.e j=&i.
Then, *j=3(pointing to the value present in i).
Then the evaluation of expression , i * *j *i + *j
3 * 3 *3 + 3 = 30

Uma mahesh said:   1 decade ago
Thanks to all great explanation.


Post your comments here:

Your comments will be displayed after verification.