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.

GrenFo said:   7 months ago
@All.
Here is the coding;

simplefied ->
// i * *j * i + *j
// i * (*j) * i + (*j)
// 3 * 3 * 3 + 3
// 30
(10)

Yash Dharane said:   9 months 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
(3)

Adi said:   2 years ago
Why &a is considered its value if &a means address of a on ram?

Please explain me.

Ajiskan said:   3 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)

SUMIT JOSHI said:   3 years ago
@All.

According to me;

i**j*i+*j,
i*(*j)*i+(*j),
3*3*3+3,
27+3,
= 30.
(9)

Jp said:   3 years ago
@ALL.


Just simplify it;

i*(*j)*i+(*j).
3*(3)*3+3.
(3)

Ksk said:   3 years ago
Thank you for explaining this @Rahul.

Shiv said:   4 years ago
Since,
i=3.
*j=content at *j means value of X.
means *j=3.
Replace i with 3 and *j also with 3.
Then, 3*3*3+3=27+3 i.e. 30.
(3)

Vijay s Rao said:   5 years 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.

Navas v said:   6 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.


Post your comments here:

Your comments will be displayed after verification.