Translation of source code (.c) into object code (.o) that can be linked with other files to be executed.
6
New cards
What are the return values?
0 = Success
Other Value = Failure
7
New cards
If-Then-Else
\
8
New cards
For Loop
9
New cards
While and Do_While
10
New cards
Do arrays have bound checking?
NO
11
New cards
How do you make a string?
An array followed by a null character.
12
New cards
Array Declaration
13
New cards
What happens when you assign out of bounds?
It loops round to the start, and adds from there.
14
New cards
Character uses ‘, what does string use?
“
15
New cards
How do you generate a random number?
rand() Generates between 0 and RAND_MAX
16
New cards
What are pseudorandom numbers?
Numbers generated by an algorithm. Look random, but aren't.
17
New cards
Does integer division round up or down?
Integer division in C rounds **down**.
18
New cards
What are the uses of Pseudorandom numbers?
Produces the same random number each time the code is run, useful for testing. Useful for most applications, but not for proper statistical modelling etc.
19
New cards
Type Conversion
Preceding an integer value with (float)converts “casts” its value into a float. In a calculation with mixed values, types are converted to that of the highest precision.
20
New cards
What is seeding of randoms?
Use of srand() before rand() to change the values used by the algorithm. Usually uses the internal clock to do so.
21
New cards
What is the function format?
myFunctionName ( )
22
New cards
Can you overload functions in C?
No, each must have unique name.
23
New cards
Why do functions need to be declared?
1\. The (one-pass) compiler needs to know a function's definition before it is called
2\. The functions may call each other. so there is no way to order all the function definitions to define them before they are called
3\. The code for the function might not be in this program file.
24
New cards
Function prototypes
A way of declaring a function before its use to allow access by all other dfunctions
25
New cards
What is the format of a function prototype?
return type, function name, parameter types (parameter names are not necessary)
26
New cards
What are Static variables
Declared using the “static” keyword
Are initialised once when the function is first called and retain their value when they go out of scope.
27
New cards
What is &x
if x is any datatype, then &x is the location in memory (its “address”) where x is currently stored
28
New cards
What does \*x do?
Declares x to contain the memory address of a variable of type
Go to the memory address stored in x and bring back the value (of type ) stored there
29
New cards
How big is a pointer?
32 bits on Windows 32-bit, 64 on Windows-64
30
New cards
Can you change pointer locations?
Yes, with pointer arithmetic. e.g., x++
31
New cards
What are void pointers?
Pointers that can point to any datatype.
They need to be cast when used:
void \*p;
p = &c;
printf("p points to %c\\n", \*(char \*)p);
32
New cards
Why might you get memory or segmentation faults?
trying to access memory to which it is not allowed access
trying to access memory in a way that is not allowed e.g. trying to overwrite the operating system
33
New cards
Why use Dynamic memory allocation
Sometimes you do not know at compile time how much memory you will need at runtime so you can dynamically allocate memory at runtime using pointers
34
New cards
How do you allocate memory dynamically
Using Malloc. You must free memory after it goes out of scope using free(p);
35
New cards
What is valgrind?
A way to test if all memory is freed properly
36
New cards
What is Pass by Value and pass by Reference?
Pass by reference uses a pointer to update the actual value. Pass by value passes the value and changes a new reference of it.
37
New cards
Can you have a pointer to a pointer?
A pointer is just another a datatype, so you can have a pointer to a pointer. Char \*\*q = &p;
38
New cards
How can you return multiple values from a function?>
Use pointers to update the value of a parameter and pass the new value back to the calling function
39
New cards
How do you pass values from command line to the
int main ( int argc, char \*\*argv ) where argc is number of arguments and argv is an array of string values.
40
New cards
Can you have a pointer to a function?
Yes!
41
New cards
What are structures?
A type of variable that groups multiple related data items together and the closest thing C has to a class
\ *struct *
*{ ;*
*;*
*};*
42
New cards
What is typedef?
A way to define structures with your own datatypes.
*typedef struct bookStruct BOOK;*
This can then be used in variable declaration.
*BOOK myBook;* (which is the same as: *struct bookStruct myBook;*)
\ \
43
New cards
What are the uses for linked lists?
When you don’t know how many elements an array should hold or if you want to store items in a non-linear structure.
44
New cards
What are linked lists?
An ordered sequence of nodes of data where each node consists of data and one or more links to other nodes in the sequence. They are memory and speed effiicnet.
45
New cards
How do you insert into an array?
To insert into an ordered array, everything must be shuffled along:. If the array is already full, then it has to be expanded first and then shuffled.
46
New cards
What are the advantages of arrays?
Can go directly to nth element. Can be iterated over using pointers.
47
New cards
What are the DISadvantages of arrays?
Inserting/Deleting requires other elements to be moved. When inserting into a full array you need to extend the array.
48
New cards
What are the advantages of linked lists?
* Does not matter where in memory nodes are stores * Insert/delete only needs to change 1 or 2 pointer values * Size of linked list is only limited by computer memory * Can link one list in multiple ways
49
New cards
What are the disadvantages of linked lists?
To find an element you must start at the beginning and follow all pointers
50
New cards
What are common types of dynamically linked lists?
\ * queues * stacks * circular lists * trees
51
New cards
How do you take user input?
scanf()
52
New cards
Properties of Streams
* An abstraction of a file that provides a constant interface to the programmer * Is buffered, so data is not necessarily written to device when issued although you can force a write by flushing the stream * Can be text stream (sequence of characters) or a binary stream (no character translation) * need to #include stdio.h
53
New cards
How do you open and close a stream?
fopen() and fclose().
\ The *fp == NULL* check if file has been successfully opened. If not, it needs error processing.
r = read, w = write, a = append (to end of text file),
r+ allows to read and write, w+ allows to create and write
Adding a b on the end (e.g., rb means to do so for binary files).
You can do multipl eiwht a + between.
55
New cards
What is errno?
A way to check if a fail occurred you can check if this has changed.
errno.h defines int errno. On startup it is 0, t
*printf("Unable to read file %s: %d: %s\n", argv[1], errno, strerror(errno));*
56
New cards
What does a call to ferror(fp) do?
* determines whether the most recent operation on the file pointed to by fp produced an error * returns 1 if an error occurred; 0 if not * value is reset by each file operation
57
New cards
How do you write to a stream?
*fputc(ch,fp)* to output char ch to file pointed to by fp
*fputs(str,fp)* to output the str to the file fp
58
New cards
How do you read from a stream?
*char ch = fgetc(fp);* to read a single character and stores in ch from file pointed to by fp
*fgets(str,count,fp);* read multiple characters up to and including newline or until maximum of *count*. Reads from file pointed to by fp and stores in str adding a null terminator.
59
New cards
What are the three standard streams (streams that don’t need to opened or closed)?
*stdin* input from terminal
*stdout* output to terminal
*stderr* output errors to terminal
60
New cards
What does the C preprocessor do?
Rewrite the file with macro expansions. Finds any files, and copies them into the position of the *#include* replacing the line.
61
New cards
What is the difference between ““ and <> for include?
<> searches for the file using the path that the CPP expects to find the file
““ searches the file first in the same directory as the current program, then the expected path.;
62
New cards
What is the use of *#define* ?
It can be used to create macros. Take care as definitions are substituted before the compiler compiles the code so are hard to trace. Names should be in all caps. Predefined macros start with “__”.
63
New cards
What is selective compilation?
A way to specify certain conditions for compilation
64
New cards
Types of storage classes
* auto (default, no need to specify): declared at the start of block, storage allocated when entered and freed when exited * register: stored in CPU registers for quick access * static: variable continues to exist after the block it is defined in terminates. value is retained even after repeated calls to the same function. Limited in scope block in which it is defined. * extern: global scope, global visibility
65
New cards
What are *#include* guards?
A way to prevent the same code running multiple times in one executable.
66
New cards
What are libraries?
They group together multiple object files (.o) into a single unit.
Static Libraries (.a, linked with and becomes part of the application at link time) and Dynamically linked shared object libraries (.so, linked to at runtime).
67
New cards
How does push work with a stack?
Add a new node with next pointing to the existing first node. Change the root to point to the new node.
68
New cards
How does pop work in a stack?
Adjust so root points to head of next node. Remove the node.
69
New cards
Stack program Structure
70
New cards
What are the two examples of jumps?
jmp = Unconditional jump
jg = jump if greater
71
New cards
How do you use jumps in C?
Use goto and a label. These must both be in same scope.
*label:*
**
*goto label;*
\ At the low level, while and for loops and constructed with conditional loops.
72
New cards
Why are goto statements frowned upon?
* Easy to write code where flow is not clear * Difficult to read and maintain * Mostly can be done in other ways
\ Cleanup and exit is the exception
73
New cards
What does the state of an executing program depend on?
* sp (stack pointer) * bp (base pointer) * PC (program counter)
74
New cards
setjmp
Saves contents of register to buffer
75
New cards
longjmp
Restores contents of registers from buffer
76
New cards
What is an enumerations?
A set of user-defined integer constants. Values start at 0 and increment by one by default.
77
New cards
What are sockets?
They enable two nodes on a network to talk to each other.
78
New cards
What happens on the client side when creating a socket?
Create a socket. Request a connection. Exchange data with server.
79
New cards
What happens on the server side when creating a socket?
1\. create a socket
2\. set socket options
3\. bind the listener process to a specific port number
4\. start listening
5\. whenever a client requests a connection
• accept a connection request from a client
• exchange data with the client send,
• close connection
80
New cards
What are pipes?
Pairs of sockets that form a temporary connection
81
New cards
What are threads?
Lightweight processes that are faster to create, switch between and communicate between (when using static variables).
82
New cards
How is multi-threading provided?
POSIX (Portable Operating System Interface). NOT be default. This creates pthreads using the POSIX threads API. pthreads is a parallel execution model independently of language.
83
New cards
What is mutex?
A locking mechanism (mutual exclusion). Can lock and unlock, for single thread access at a time.
84
New cards
What is semaphore?
A signalling mechanism. Allows multiple threads simultaneously access to a resource at one time. by initialising semaphore as integer.
Wait: If count is 0, it waits until it isn’t. Decreases semaphore count by one.
signal: Increments semaphore count by one to show it is finished with recourse.
85
New cards
What are the 3 Unix standards?
POSIX (Portable Operating System Interface), UNIX and Linux (has many varieties but the de faco standards are Rad Hat Linux and Ubuntu)
86
New cards
What is POSIX?
Enables developers to write portable applications. It is a family of standards that maintains compatibility between different variants of UNIX.
87
New cards
What does POSIX define?
* Application programming interface (API)
* Command Line shells * Utility Interfaces
\ Describes a set of fundamental services needed for applications:
* An interface (in C) * A command interpreter (shell) * Common utility programs * Standard semantics and syntax
88
New cards
What is the Open group?
Established in 1999, with members such as Oracle, IBM, HP. Aims to create a single UNIX specification to ensure compatibility across platforms.
89
New cards
What are the three layers of UNIX?
Programs, Shell and Kernel
90
New cards
What is a process?
Everything in unix is a file (passive) or process (active). Every process has a unique Process ID (PID), one parent (except system **swapper** which has PID 0 and is the ancestor of all processes) , and zero or more children (spawned when fork command is used.
91
New cards
What does the ps command do?
Process snapshot, gives snapshot of all processes running on the terminal. | is a pipe.
92
New cards
What does *top* do?
Shows real time list of all processes.
93
New cards
What is the difference between foreground and background?
Foreground - shells waits until process finished.
Background - can continue interacting with shell until process runs, process is killed when parent process finishes. To run process in background: *$ tempsleep &*
94
New cards
Priority
All processes run with a priority. You should ensure background processes are lower priority than foreground by using *nice* and *renice.*
95
New cards
Forking in C
96
New cards
Parent waiting for child to finish
\
97
New cards
Exec
A process can replace itself by using one of the *exec* functions which is a wrapped for *execve.*
98
New cards
Pipe
For parent-child communications, it must be opened before the fork, so both parent and child can share it.They are unidirectional, data can be transferred and processes must be related (parent & child/siblings).
99
New cards
Signals
Another way to communicate between processes.
e.g., *kill* command. Enables you to terminate background process by specifying PID or process number. *$ kill 10748*
No data is transferred, and you need to know the PID.
100
New cards
Physical Memory Addressing
Used for single-tasking operating systems such as MS-DOS.