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.

Soumen Maity said:   1 decade ago
In printf() the operation should be right to left. I don't why you guys are doing left to right operation.

Nishu.. said:   1 decade ago
i=3,
*j=3,
Because j = &i;
Therefore i**j*i+*j = 3*3*3+3 = 30.

John Jose said:   1 decade ago
j=&i implies *j=i;

*j=3.
i**j=3*3 = 9.
i**j*i=9*3 = 27.
i**j*i+*j = 27+3 = 30.

SaiBheema said:   1 decade ago
i=3 and j=3, since j=&i.

[((i*(*j))*i)+*j]=[((3*3)*3)+3].

= 30.

Samneet said:   1 decade ago
@Sudhakar.

i**j*i+*j*i.

*(dereference operator) has higher precedence than the arithmetic operators.

So during the evaluation of the expression:

Step1: i*(*j)*1+(*j)*i.

Since,*j=3 and i =3.

Step2: i*3*i+3*i.

Step3: (3*3)*3+(3*3).

Step4: 9*3+9.

Step5: 27+9.

Step: 36.

Hope you got this.

Sudhakar said:   1 decade ago
I have one more doubt.

i**j*i+*j=i*(*j)*(i)+(*j)

*j=i=3
Then ((3*3)*3)+3=30
Upto this it will be k.

Add 1 more *i to the above given means
i**j*i+*j*i=should be 30*3=90.

But the output is showing as 36 reason please?

Spurthi said:   1 decade ago
Here i**j=3*3=9.

i**j*i=9*3=27.

i**j*i+*j=27+3=30.

Dipankar Pramanik said:   1 decade ago
Here i=3 and *j=3.
So i *(multiplication) *j=3x3=9.

Then 9 * (mul) i=9x3=27.
At last 27 + (add) *j=27+3=30.

Mukesh said:   1 decade ago
j contains address of i.

*j contains the value of i; i.e; 3

The expression then becomes i*(*j)*i+(*j)=3*3*3+3=30.

Hence the answer is 30.

Hope this helps.

Rishabh said:   1 decade ago
>> Simple thumb rule.

When * and & are one after another, they cancel each other.

Example: *(&i)=&(*i)=i

Now, j=&i (given)

Therefore, *j=*(&i)=i

I hope the rest is understood.


Post your comments here:

Your comments will be displayed after verification.