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 5 of 7.
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.
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.
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.
Nishu.. said:
1 decade ago
i=3,
*j=3,
Because j = &i;
Therefore i**j*i+*j = 3*3*3+3 = 30.
*j=3,
Because j = &i;
Therefore i**j*i+*j = 3*3*3+3 = 30.
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.
Binny said:
1 decade ago
Well done it was a trick as i, i is not a pointer so how we can interpret 2nd* as pointer.
Nap said:
1 decade ago
Answer = 30 because,
i**j*i+*j = (3*3*3+3).
Where i=3 and *j=3.
i**j*i+*j = (3*3*3+3).
Where i=3 and *j=3.
Neha said:
1 decade ago
Ok but why do we try out and make j=3.
Hemanth Kumar M said:
1 decade ago
*j -> 3.
i = 3.
Therefore 3*3*3+3.
Due to associativity from l to f.
*-> has highest priority.
+-> next priority.
Hence 9*3+3 = 27+3.
->30.
i = 3.
Therefore 3*3*3+3.
Due to associativity from l to f.
*-> has highest priority.
+-> next priority.
Hence 9*3+3 = 27+3.
->30.
Subroto Biswas said:
1 decade ago
It's simple:
= 3*3*3+3.
So, 9*3+3 = 27+3.
Finally, 30.
= 3*3*3+3.
So, 9*3+3 = 27+3.
Finally, 30.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers