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

Yash Dharane said:   2 years ago
@All.

Here's my explanation.

#include<stdio.h>
int main()
{
int i=3, *j, k;
j = &i;
printf("%d\n", i**j*i+*j);
return 0;
}
int i=3, *j, k;: This line declares three variables: i is an integer initialized with the value 3, j is a pointer to an integer (not initialized), and k is an integer (not initialized).

j = &i;: This line assigns the address of the variable i to the pointer j. Now, j points to the memory location of i.
printf("%d\n", i**j*i+*j);: This line prints the result of the expression i**j*i+*j as an integer. Let's break this down:

i: The value of i is 3.
*j: Dereferencing j gives the value stored in the memory location pointed to by j, which is the value of i.
So, *j is also 3.
i**j: This is the multiplication of i and *j. So, it's equal to 3 * 3, which is 9.
i**j*i: This is the multiplication of i**j and i. So, it's equal to 9 * 3, which is 27.
*j: The value of *j is still 3.
i**j*i+*j: This is the addition of i**j*i and *j. So, it's equal to 27 + 3, which is 30.
The printf statement prints the value 30 followed by a newline character (\n) to the standard output.

So, when you run this program, it will print: 30
(8)

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

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

Navas v said:   7 years ago
Here using precedence of operator first evaluate * (dereferencing operator) associativity from right to left.

So expression become i*(*J)*i+(*j) ->> i*(3)*i+(3).

Then precedence goes to * (multiplication operator) and their associativity from left to right
so it become ((i*3)*i)+3--> 3*3*3+3=27.

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.

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

Ajiskan said:   4 years ago
i**j*i+*j represent as;

i*(*j)*i+(*j) \\ *j dereference and gives value 3 \\ after that rule of precedence table.
First multiplication after that addition operator worked and final output displayed.

(3*3*3)+3 \\subtraction.
27+3 \\additon.
30 -> output.
(8)

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

Dpkbohra said:   1 decade ago
int i=3, *j, k;
j = &i;
printf("%d\n", i**j*i+*j);
return 0;
}

here,
i=3
and j= &i i.e j=3

and i**j*i+*j = i*(*j)*(i)+(*j)
= 3*(3)*(3)+(3)
= 27+3
= 30 Ans.

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?


Post your comments here:

Your comments will be displayed after verification.