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

SUMIT JOSHI said:   4 years ago
@All.

According to me;

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

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)

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

Please explain me.

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)

GrenFo said:   2 years ago
@All.
Here is the coding;

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


Post your comments here:

Your comments will be displayed after verification.