Week 02 Tutorial Questions
-
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
-
Show what the following decimal values look like in 8-bit binary, 3-digit octal, and 2-digit hexadecimal:
1
8
10
15
16
100
127
200
How could I write a C program to answer this question?
-
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:
a | b
(bitwise OR)a & b
(bitwise AND)a ^ b
(bitwise XOR)a & ~b
(bitwise AND)c << 6
(left shift)a >> 4
(right shift)a & (b << 1)
b | c
a & ~c
Give your answer in hexadecimal, but you might find it easier to convert to binary to work out the solution.
-
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
unsigned char device;
Write C expressions to implement each of the following:
- mark the device as locked for reading bytes
- mark the device as locked for writing blocks
- set the device as locked, leaving other flags unchanged
- remove the lock on a device, leaving other flags unchanged
- switch a device to/from reading and writing, leaving other flags unchanged
-
Discuss the starting code for
sixteen_out
, one of this week's lab exercises. In particular, what does this code (from the providedmain
) 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);
-
How does the C library function
void *realloc(void *ptr, size_t size);
void *malloc(size_t size);
-
Given the following type definition
typedef unsigned int Word;
Write a function
Word reverseBits(Word w);
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. -
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.
-
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:
-
Which variables are accessible from within
main()
? -
Which variables are accessible from within
fun2()
? -
Which variables are accessible from within
fun1()
? -
Which variables are removed when
fun1()
returns? -
Which variables are removed when
fun2()
returns? -
How long does the variable
f
exist during program execution? -
How long does the variable
g
exist during program execution?
-
-
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:
-
p = &x;
-
x = 5;
-
*p = 3;
-
x = (int)p;
-
x = (int)&p;
-
p = NULL;
-
*p = 1;
If any of the statements would trigger an error, state what the error would be.
-
-
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 aStack s
parameter.-
Why might we define the parameters differently like this?
-
Assuming that the stack parameter's name is
s
, how would you refer to thetop
field within the functioninitStack()
and within the functionisEmptyStack()
? -
Are there any disadvantages to the parameter type used by
isEmptyStack()
andshowStack()
?
-
-
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.