412 views
8 8 votes

What is the output of the following code?

#include <stdio.h>

int main() {
    int x = 10;
    int *p = &x;
    int **q = &p;

    **q = **q + 4;
    *p = *p + 1;

    printf("%d %d", x, **q);

    return 0;
}
  1. $\texttt{14 14}$
     
  2. $\texttt{15 15}$
     
  3. $\texttt{10 15}$
     
  4. $\texttt{Compilation error}$

3 Answers

0 0 votes

Here, $\texttt{p}$ points to $\texttt{x}$.

Also, $\texttt{q}$ points to $\texttt{p}$.

So,

$\texttt{*q}$ gives $\texttt{p}$

and

$\texttt{**q}$ gives the value of $\texttt{x}$.

Initially:

$\texttt{x = 10}$

First statement:

$\texttt{**q = **q + 4}$

Since $\texttt{**q}$ refers to $\texttt{x}$,

$\texttt{x = 10 + 4 = 14}$

Second statement:

$\texttt{*p = *p + 1}$

Since $\texttt{p}$ points to $\texttt{x}$,

$\texttt{x = 14 + 1 = 15}$

Now,

$\texttt{x = 15}$

and

$\texttt{**q = 15}$
 

$\therefore$ Output : $\texttt{15 15}$
 

Answer: B

Answer:
Position:
Show:

Related questions

7 7 votes
3 3 answers
379
379 views
GO Classes asked Jun 25
379 views
What is the output of the following code?#include <stdio.h void update(int q, int *r) { q = q + 5; *q = r; q = q + 2; } int main() { int x = 3, y = 8; int *p = &x; u...
6 6 votes
2 2 answers
349
349 views
GO Classes asked Jun 25
349 views
What is the output of the following code?#include <stdio.h int main() { int a[3] = { {1, 2}, {3, 4}, {5, 6} }; int (*p) = a; p++; printf("%d %d %d", (*p)[0], *(*(p + 1)...
9 9 votes
3 3 answers
406
406 views
GO Classes asked Jun 25
406 views
What is the output of the following code?#include <stdio.h int main() { int a = 5, b = 9; int *p = &a; int q = &p; *p = *p + 2; *q = &b; q = q + 3; printf("%d %d %d", ...
8 8 votes
3 3 answers
462
462 views
GO Classes asked Jun 25
462 views
What is the output of the following code?#include <stdio.h int main() { int a [3] = { {2, 4, 6}, {8, 10, 12} }; printf("%d %d %d", a , *(*(a + 1) + 1), *(*a + 2)); retur...