What is the difference between 'a'
and "a"
?
Does the following code terminate? And if so, under which conditions?
char i, j; for (i = 0; i < 10, j < 5; i++, j++);
Write an implementation of bubble sort for a fixed array of integers. An array of integers can be defined as int i[] = {1, 2, 3, 4};
Use function recursion to write an implementation of merge sort for a fixed array of integers. How much memory does your program use for a list of length n?
Implement a function int cntlower(char str[])
that, given a string, returns the number of lower-case letters it contains.
Define a macro SWAP(t, x, y)
that swaps two arguments x
and y
of type t
. Does your macro work as expected when called as SWAP(int, v[i++], w[f(x)])
?
What other invocations could cause a simple implementation to fail and how can we mitigate them?
Define a macro SWAP(x, y)
that swaps x
and y
of the same type without using any extra memory.
Given a pointer p
, what does the expression p[-2]
mean, and when is it legal?
Write a string search function with a declaration of
char *strfind(const char *s, const char *f);
which returns a pointer to the first occurrence of s
in f
(and NULL
otherwise)
Write a program calc
which evaluates an expression in Reverse Polish Notation given on the command line; for example
$ calc 2 3 4 + \*
should print 14. The character *
needs to be escaped with a backslash in some shells or otherwise it gets expanded to all the files in your current directory.
What is the value of i
after executing each of the following:
i = sizeof(char);
i = sizeof(int);
int a; i = sizeof a;
char b[5]; i = sizeof(b);
char *c=b; i = sizeof(c);
struct { int d; char e;} s; i = sizeof s;
void f(int j[5]) { i = sizeof j;}
void f(int j[][10]) { i = sizeof j; }
A spacecraft arrives on Mars, but its memory has been corrupted by radiation en
route. Luckily, it can receive updates one bit at a time using a predefined C
function short receive_bit(void)
, that when called will return either 1 or 0.
The stream of bits for a value is transmitted in unsigned big-endian byte order:
for example, a 16-bit value of 125 would be 0000000001111101. Assume the int
type is 32 bits.
Explain the meaning of the inline
keyword on C function declarations, and a
potential drawback of using it.
Using receive_bit()
, define a function receive_int()
that decodes and
returns a 32-bit value from the sequence of received bits.
(optional) Build a more general decoding function receive using a C++ template with two parameters that specify the number of bits to decode and a datatype for the decoded value. Use this to write two template instantiations that decode an 8-bit value into a short and a 32-bit value into an unsigned long.
Find and explain four instances of undefined behaviour that could result
from compiling and running the C code below with different command-line
arguments.
The strcpy(dst,src)
function copies a zero-terminated C string from the
src
buffer to the dst
buffer. The putchar(c)
function outputs a
character c to the console. You can assume that the standard C header
prototypes have been included for <stdio.h>
, <stdlib.h>
and <string.h>
.
char *show_instruction(int msg) { char buf[6]; int fuel; if (msg == 1 && fuel--) { strcpy(buf, "THRUST"); return buf; } else if (msg == 2) { char *msg = (char *)malloc(100); strcpy(msg, "DEPLOY_PARACHUTE"); return msg; } } int main(int argc, char **argv) { char *msg; msg = show_instruction(argc); putchar(msg[0]); return 0; }