What will be output of following program? #include<stdio.h> int main(){ int i…
What will be output of following program?
#include<stdio.h>
int main(){
int i = 5 , j;
int p , q;
p = &i;
q = &j;
j = 5;
printf("%d %d",*p,*q);
return 0;
}
Answer: D. Compilation Error — Answer: Compilation Error Problem: p and q are declared as int but are used as pointers. The code does p = &i; q = &j; and then uses *p and *q. Assigning &i…
- A.
5 5
- B.
Address Address
- C.
5 Address
- D.
Compilation Error
Attempted by 349 students.
Show answer & explanation
Correct answer: D
Answer: Compilation Error
Problem: p and q are declared as int but are used as pointers. The code does p = &i; q = &j; and then uses *p and *q.
Assigning &i (an address) to an int variable is an incompatible pointer/integer assignment and will cause a compiler diagnostic.
Applying the unary * operator to a variable of type int is invalid; * requires a pointer operand. Compilers typically error with messages like 'invalid type argument of unary *'.
Therefore the program will not compile as written.
If the intention was to use pointers, declare p and q as pointer types. For example:
int i = 5, j;
int *p, *q; // p and q are pointers
p = &i; q = &j; j = 5; printf("%d %d", *p, *q); // prints 5 5
Conclusion: The original code is invalid and leads to a compilation error. After correcting the declarations to pointers, the program prints 5 5.