in Programming edited by
147 views
0 votes
0 votes

Consider the two $\mathrm{C}$ programs given below.

C-CODE (I)

 #include<stdio.h> 
 int main() {
    int n=2, *ptr=&n ; n*=3; 
    printf ("%d", (*ptr**ptr)*(*ptr**ptr)); 
    }

C-CODE(II)

#include<stdio.h> 
int main() {
    int n=2, *ptr=&n ; n*=3; 
    printf ("%d", *ptr**ptr**ptr**ptr); 
    }

Given $\mathrm{C}$ codes (I) and (II) above, decide which of the following statements is true.

  1. Output of (I) is $36$ and (II) is $216$
  2. Output of (I) is $216$ and (II) is $1296$
  3. Output of (I) is $1296$ and (II) is $216$
  4. Output of both (I) and (II) is $1296$
  5. None of the above
in Programming edited by
147 views

1 Answer

0 votes
0 votes

Before $printf$ (for both programs), $n=6$ and $*ptr$ points to $n$ ie now $n \equiv *ptr$ in both the programs.

($*$ dereference operator has higher precedence than $*$ multiplication operator)

In program $1$,

$(*ptr * *ptr) * (*ptr * *ptr) \equiv ((*ptr)*(*ptr))*((*ptr)*(*ptr)) \equiv (n*n)*(n*n)$

In program $2$,

$*ptr**ptr**ptr**ptr \equiv (*ptr)*(*ptr)*(*ptr)*(*ptr) \equiv n*n*n*n$

($6^4 = 1296$)

$\therefore$ both programs print $1296$.

Answer :- D.

Related questions