• retagged by
372 views
6 6 votes

What is the output of the following code?

#include <stdio.h>

struct Student {
    int roll;
    int marks;
};

int main() {
    struct Student s[3] = {
        {1, 70},
        {2, 80},
        {3, 90}
    };

    s[1].marks = s[1].marks + 5;
    s[2].roll = s[0].roll + s[1].roll;

    printf("%d %d %d", s[1].marks, s[2].roll, s[2].marks);

    return 0;
}
  1. $\texttt{80 3 90}$
     
  2. $\texttt{85 3 90}$
     
  3. $\texttt{85 2 90}$
     
  4. $\texttt{85 3 95}$

2 Answers

0 0 votes

Here, $\texttt{s}$ is an array of structures.

Each element like $\texttt{s[0]}$, $\texttt{s[1]}$, and $\texttt{s[2]}$ is one complete $\texttt{struct Student}$ object.

Initially:

$\texttt{s[0].roll = 1}$, $\texttt{s[0].marks = 70}$

$\texttt{s[1].roll = 2}$, $\texttt{s[1].marks = 80}$

$\texttt{s[2].roll = 3}$, $\texttt{s[2].marks = 90}$


Now:

$\texttt{s[1].marks = s[1].marks + 5;}$

So,

$\texttt{s[1].marks = 80 + 5 = 85}$


Next:

$\texttt{s[2].roll = s[0].roll + s[1].roll;}$

So,

$\texttt{s[2].roll = 1 + 2 = 3}$

$\texttt{s[2].marks}$ remains unchanged, so it is still $\texttt{90}$.


Therefore, the output is:

$\texttt{85 3 90}$


Answer: B

Answer:
Position:
Show:

Related questions

7 7 votes
3 3 answers
375
375 views
GO Classes asked Jun 29
375 views
What will happen when the following code is compiled?#include <stdio.h struct Point { int x; int y; }; int main() { struct Point p1 = {2, 4}; struct Point p2; p2 = p1; p2...
7 7 votes
2 2 answers
356
356 views
GO Classes asked Jun 29
356 views
What is the output of the following code?#include <stdio.h struct Item { int code; int price; }; void update(struct Item *p) { p->price = p->price + 20; (*p).code = (*p)....
6 6 votes
2 2 answers
327
327 views
GO Classes asked Jun 29
327 views
What is the output of the following code?#include <stdio.h struct Box { int width; int height; }; int main() { struct Box b = {4, 6}; struct Box *p = &b; p->width = p->wi...
6 6 votes
2 2 answers
341
341 views
GO Classes asked Jun 29
341 views
What is the output of the following code?#include <stdio.h struct Book { char name; float price; int pages; }; int main() { struct Book b1 = {'C', 150.5, 300}; b1.pages =...