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 ErrorAnswer: 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…

  1. A.

    5 5

  2. B.

    Address Address

  3. C.

    5 Address

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

Explore the full course: Gate Guidance By Sanchit Sir

Loading lesson…