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 1 of 7.
GrenFo said:
2 years ago
@All.
Here is the coding;
simplefied ->
// i * *j * i + *j
// i * (*j) * i + (*j)
// 3 * 3 * 3 + 3
// 30
Here is the coding;
simplefied ->
// i * *j * i + *j
// i * (*j) * i + (*j)
// 3 * 3 * 3 + 3
// 30
(24)
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.
According to me;
i**j*i+*j,
i*(*j)*i+(*j),
3*3*3+3,
27+3,
= 30.
(10)
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
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)
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.
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)
Jp said:
4 years ago
@ALL.
Just simplify it;
i*(*j)*i+(*j).
3*(3)*3+3.
Just simplify it;
i*(*j)*i+(*j).
3*(3)*3+3.
(3)
Shiv said:
6 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.
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)
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.
Satti(iiit) said:
1 decade ago
Very nice munny. I am getting your explanation.
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.
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers