Week 02 Tutorial Questions

  1. When should the types in stdint.h be used:
    #include <stdint.h>
    
                     // range of values for type
                     //             minimum               maximum
        int8_t   i1; //                 -128                  127
        uint8_t  i2; //                    0                  255
        int16_t  i3; //               -32768                32767
        uint16_t i4; //                    0                65535
        int32_t  i5; //          -2147483648           2147483647
        uint32_t i6; //                    0           4294967295
        int64_t  i7; // -9223372036854775808  9223372036854775807
        uint64_t i8; //                    0 18446744073709551615
    
  2. Show what the following decimal values look like in 8-bit binary, 3-digit octal, and 2-digit hexadecimal:

    1. 1
    2. 8
    3. 10
    4. 15
    5. 16
    6. 100
    7. 127
    8. 200

    How could I write a C program to answer this question?

  3. Assume that we have the following 16-bit variables defined and initialised:

    uint16_t a = 0x5555, b = 0xAAAA, c = 0x0001;
    

    What are the values of the following expressions:

    1. a | b (bitwise OR)
    2. a & b (bitwise AND)
    3. a ^ b (bitwise XOR)
    4. a & ~b (bitwise AND)
    5. c << 6 (left shift)
    6. a >> 4 (right shift)
    7. a & (b << 1)
    8. b | c
    9. a & ~c

    Give your answer in hexadecimal, but you might find it easier to convert to binary to work out the solution.

  4. Consider a scenario where we have the following flags controlling access to a device.

    #define READING   0x01
    #define WRITING   0x02
    #define AS_BYTES  0x04
    #define AS_BLOCKS 0x08
    #define LOCKED    0x10
    
    The flags are contained in an 8-bit register, defined as:
    unsigned char device;
    

    Write C expressions to implement each of the following:

    1. mark the device as locked for reading bytes
    2. mark the device as locked for writing blocks
    3. set the device as locked, leaving other flags unchanged
    4. remove the lock on a device, leaving other flags unchanged
    5. switch a device to/from reading and writing, leaving other flags unchanged
  5. Discuss the starting code for sixteen_out, one of this week's lab exercises. In particular, what does this code (from the provided main) do?

        long l = strtol(argv[arg], NULL, 0);
        assert(l >= INT16_MIN && l <= INT16_MAX);
        int16_t value = l;
    
        char *bits = sixteen_out(value);
        printf("%s\n", bits);
    
        free(bits);
    
  6. How does the C library function
    void *realloc(void *ptr, size_t size);
    
    differ from
    void *malloc(size_t size);
    
  7. Given the following type definition

    typedef unsigned int Word;
    

    Write a function

    Word reverseBits(Word w);
    
    ... which reverses the order of the bits in the variable w.

    For example: If w == 0x01234567, the underlying bit string looks like:

    0000 0001 0010 0011 0100 0101 0110 0111
    

    which, when reversed, looks like:

    1110 0110 1010 0010 1100 0100 1000 0000
    

    which is 0xE6A2C480 in hexadecimal.

  8. If the following program is in a file called prog.c:

    #define LIFE 42
    #define VAL random() % 20
    
    #define sq(x) (x * x)
    #define woof(y) (LIFE + y)
    
    int main(void) {
        char s[LIFE];
        int i = woof(5);
        i = VAL;
        return (sq(i) > LIFE) ? 1 : 0;
    }
    

    … then what will be the output of the following command:

    gcc -E prog.c
    

    You can ignore the additional directives inserted by the C pre-processor.

  9. Consider the following C program skeleton:
    int  a;
    char b[100];
    
    int fun1() { int c, d; ... }
    
    double e;
    
    int fun2() { int f; static int ff; ... fun1() ... }
    
    unsigned int g;
    
    int main(void) { char h[10]; int i; ... fun2() ... }
    

    Now consider what happens during the execution of this program and answer the following:

    1. Which variables are accessible from within main()?

    2. Which variables are accessible from within fun2()?

    3. Which variables are accessible from within fun1()?

    4. Which variables are removed when fun1() returns?

    5. Which variables are removed when fun2() returns?

    6. How long does the variable f exist during program execution?

    7. How long does the variable g exist during program execution?

  10. Consider the following pair of variables

    int  x;  // a variable located at address 1000 with initial value 0
    int *p;  // a variable located at address 2000 with initial value 0
    

    If each of the following statements is executed in turn, starting from the above state, show the value of both variables after each statement:

    1. p = &x;

    2. x = 5;

    3. *p = 3;

    4. x = (int)p;

    5. x = (int)&p;

    6. p = NULL;

    7. *p = 1;

    If any of the statements would trigger an error, state what the error would be.

  11. Consider a Stack data type like the one defined in lectures:

    // Interface to Stack data type
    
    #define MAX_STACK 1000
    
    typedef char Item;
    
    typedef struct _stack {
        int top;
        Item items[MAX_STACK];
    } Stack;
    
    void initStack (Stack *s);
    int pushStack (Stack *s, Item val);
    Item popStack (Stack *s);
    int isEmptyStack (Stack s);
    void showStack (Stack s);
    

    Some of the functions have a parameter defined as Stack *s, while others have a Stack s parameter.

    1. Why might we define the parameters differently like this?

    2. Assuming that the stack parameter's name is s, how would you refer to the top field within the function initStack() and within the function isEmptyStack()?

    3. Are there any disadvantages to the parameter type used by isEmptyStack() and showStack()?

  12. Write a program, using the Stack data type defined above, to determine whether text on standard input contains all matching brackets. Brackets includes parentheses (the ( and ) pair), (square) brackets (the [ and ] pair), and (curly) braces (the { and } pair). Matching means that whenever you see a closing bracket, the most recently opened bracket was of the corresponding type (e.g., if we see a ], the most recent opening bracket must be [). The program should simply ignore all characters that are not brackets.

    Some conditions to consider: (a) there may be more opening brackets than closing brackets, (b) there may be fewer opening brackets than closing brackets, (c) brackets too deeply nested, (d) closing bracket doesn't match most recent opening bracket. Print a suitable error message and terminate the program whenever an error is detected.