Assignment - I

Due Date: Monday 15 September 2025 (7:00 PM)


Instructions

  1. Answer all the questions. Marks are given alongside each question.
  2. You must write your own code to support your answers wherever necessary.

Question No. 1

Marks: 5
  • Examine the code in the cell below. Save it into a file called question-01.c
  • Compile and run the code.
  • Explain the output using operator precedences.
  • Examine Line No. 24 and see if you can change it to get the correct output. Do not use any brackets! Use operator precedence rules to correct it.
In [ ]: 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdio.h>
#include <stdlib.h>

typedef struct {
     float first;
     float end;
} Node;

int main(void)
{
     Node p[5], *q;
     int i;
     float a, b;
     
     for (i=0; i<5; i++) {
          p[i].first = 3.14 + i;
          p[i].end = 7.89;
     }

     q = &p[0];
     i = 0;
     while(i < 5) {
          a = q->first;
          b = ++q->end;
          printf("\tFirst: %f\tLast: %f\n", a, b);
          i += 1;
     }

     exit(0);
}

Question No. 2

Marks: 5
How does gcc allocate variables in memory?
Write a small code segment to declare the following variables and then print their addresses.
int p, q, r;
int a;
double b;
int c;
After noting the addresses, write another program segment as:
int c, b, a;
int r;
double q;
int p;
Again note down the addresses. What are your findings? You may also want to try this experiment on a non-gcc compiler.

Question No. 3

Marks: 6
What are the outputs in the following three cases when using a gcc compiler? If you want to, you can initialise the variables b and c between $\pm50$. In the first two cases, have a printf() for printing the values of a, b, c and d immediately after assignment to b.
int a = 10, b, c, d = 1;
Case 1: b = 2 * a + c - (a = 28 * d);
Case 2: b = 2 * a + c - a, a = 28 * d;
Case 3: printf ("a: %d\tb:%d\tc:%d\td:%d\n", a, b = 2 * a + c - (a = 28 * d), c, d = d + 2);
Explain clearly how control flows within a statement using the results.

Question No. 4

Marks: 4
Write a simple for loop in C as shown below. The general syntax of for is
for (<s1>; <cond>;<s2>)
When exactly are these three statements executed?
How do break and a continue affect the execution of <s2>?

THE END