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;
}
Discussion:
65 comments Page 2 of 7.
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.
*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.
[((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.
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?
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.
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.
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.
*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.
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.
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.
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.
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.
Here i=3, *j also 3 according to the precedence order it will execute like this.
(3) * (3) * (3) + (3) =30.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers