Question discussion

What is the output of the following C program?#include struct node { int v; struct node *next; }; int main() { struct node a = {1, 0}, b =…

1530Medium DSSSB TGT Computer Science 0 replies 1 views
0

What is the output of the following C program?

#include <stdio.h>
struct node { int v; struct node *next; };
int main() {
    struct node a = {1, 0}, b = {2, 0}, c = {3, 0};
    a.next = &b; b.next = &c;
    struct node *p = &a;
    int s = 0;
    while (p) { s += p->v; p = p->next; }
    printf("%d %d", s, a.next->next->v);
    return 0;
}
  • A3 3
  • B6 2
  • C6 3
  • D3 6
Correct answer: C

Explanation

The loop visits the three nodes and adds 1 + 2 + 3 = 6; a.next->next is c, whose value is 3.

Discussion 0

Nobody has replied yet. Start the discussion!