edited by
372 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
edited by

1 Answer

1 votes
1 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

1 votes
1 votes
1 answer
1
admin asked Aug 18, 2022
440 views
What will be the output of the following C program? Justify your answer. Negative numbers are represented in $2$'s complement,#include<stdio.h int main() { if (-~-1) prin...
1 votes
1 votes
3 answers
2
admin asked Aug 18, 2022
528 views
What does the following function compute for $x \neq 0?$float isi1(float x, int y) { if (y==0) { return 1; } else if (y>0) { return isi1(x,-y); } else { return isi1(x, y+...
1 votes
1 votes
1 answer
3
admin asked Aug 8, 2022
422 views
Simplify the following Boolean function in product-of-sums form: $$ F(A, B, C, D)=\sum(0,1,2,5,8,9,10) . $$
0 votes
0 votes
0 answers
4
admin asked Aug 25, 2022
300 views
Consider the following state diagram of a sequential circuit, where each of a, b, c, d, e, f and g represents a state. Represent thestate diagram with minimum number of s...