comp1521 – Week 02 Tutorial Questions (Solution)

$ 20.99
Category:

Description

1. When should the types in stdint.h be used:

2. Show what the following decimal values look like in 8-bit binary, 3-digit octal, and 2-digit hexadecimal: a. 1
b. 8
c. 10
d. 15
e. 16
f. 100
g. 127
h. 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:

What are the values of the following expressions:
a. a | b (bitwise OR)
b. a & b (bitwise AND)
c. a ^ b (bitwise XOR)
d. a & ~b (bitwise AND)
e. c << 6 (left shift)
f. a >> 4 (right shift)
g. a & (b << 1)
h. b | c
i. 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.

The flags are contained in an 8-bit register, defined as:

Write C expressions to implement each of the following:
a. mark the device as locked for reading bytes
b. mark the device as locked for writing blocks
c. set the device as locked, leaving other flags unchanged
d. remove the lock on a device, leaving other flags unchanged
e. 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?

6. Given the following type definition

Write a function

… 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.

Revision questions
The remaining tutorial questions are primarily intended for revision – either this week or later in session.
7. Consider the following small C program:

Assume the variable n has address 0x7fff00000000.
Assume sizeof (int) == 4.
What does the program print?
8. What is the output from the following program and how does it work? Try to work out the output without copy-paste-compileexecute.

9. Consider the following struct definition defining a type for points in a three-dimensional space:

and the program fragment using Coord variables and pointers to them:

a. Draw diagrams to show the state of the variables a, b, and p, at points A and B.
b. Why would a statement like *p.x++; be incorrect?
c. Write code to iterate over the coords array using just the pointer variable p the address of the end of the array, and setting each item in the array to (0,0,0). Do not use an index variable.
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:
a. p = &x;
b. x = 5;
c. *p = 3;
d. x = (int)p;
e. x = (int)&p;
f. p = NULL;
g. *p = 1;
If any of the statements would trigger an error, state what the error would be.
11. Consider the following C program skeleton:

Now consider what happens during the execution of this program and answer the following:
a. Which variables are accessible from within main()?
b. Which variables are accessible from within fun2()?
c. Which variables are accessible from within fun1()?
d. Which variables are removed when fun1() returns? e Which variables are removed when fun2() returns?
e. Which variables are removed when fun2() returns?
f. How long does the variable f exist during program execution?
g. How long does the variable g exist during program execution?
12. Explain the differences between the properties of the variables s1 and s2 in the following program fragment:

Where is each variable located in memory? Where are the strings located?
13. How does the C library function

differ from

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

… 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.
15. What is the effect of each of the static declarations in the following program fragment:

16. What is the difference in meaning between the following pairs (a/b and c/d) of groups of C statements:
a. if (x == 0) { printf (“zero “);
}
b. if (x == 0) printf (“zero “);
c. if (x == 0) { printf (“zero “); printf (“after “);
}
d. if (x == 0) printf (“zero “); printf (“after “);
17. C functions have a number of different ways of dealing with errors:
terminating the program entirely (rare) setting the system global variable errno returning a value that indicates an error (e.g., NULL, EOF) setting a returning parameter to an error value
They might even use some combination of the above.
Think about how the following code might behave for each of the inputs below. What is the final value for each variable?

Inputs:
a. 42 64 999
b. 42 64.4 999
c. 42 64 hello
d. 42 hello there
e. hello there
18. Consider a function get_int() which aims to read standard input and return an integer determined by a sequence of digit characters read from input. Think about different function interfaces you might define to deal with input that is not a sequence of digits, or that is a very long sequence of digits.
19. For each of the following commands, describe what kind of output would be produced: a. gcc -E x.c
b. gcc -S x.c
c. gcc -c x.c
d. gcc x.c
Use the following simple C code as an example:

For all enquiries, please email the class account at cs1521@cse.unsw.edu.au
CRICOS Provider 00098G

Reviews

There are no reviews yet.

Be the first to review “comp1521 – Week 02 Tutorial Questions (Solution)”

Your email address will not be published. Required fields are marked *