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 3 of 7.

Preeti said:   1 decade ago
Thanx Ajay Kumar Varma..explanation was great!!

Raz said:   1 decade ago
i=3;
*j=3;

So given statement is read by compiler as.
[(i)*(*j)*(i)+(*j)]
[(3)*(3)*(3)+(3)]
[27+3]
[30]

Zohra said:   1 decade ago
Well explained. Munny and Nihar. Thank You.

Priya said:   1 decade ago
i**j*i+*j
evaluation is done according to the priority.

(like int i,j;
int h=i*j/j;
here * will done then / will process.)


we have 2 different i*h(means multiple),*j (means value of pointer at address)

*j has high priority than multiple.


i**j*i+*j
step1 i*3*i+*j
step2 i*3*i+3
step3 3*3*i+3=9*i+3
step4 9*3+3=27+3
step5 30

Aakash said:   1 decade ago
Here i=3
j=&i;
so *j=3

Now the expression:

i**j*i+*j=i*(value at j)*i+(value at j)=3*3*3+3=27+3=30

Since *has higher precedence over +

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

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

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

Sai said:   1 decade ago
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


Post your comments here:

Your comments will be displayed after verification.