Lecture 9 - Programming Flashcards

1
Q

What is the difference between C and Assembly (What are the advs of high level languages (ie C))?

A
  1. Machine independent - programs can be ported to different computer systems
  2. Easier to program and debug - cheaper software development (one line of C is several lines of assembly)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does C do to run the code?

A

Essentially C checks everything first, hence detecting errors at the very beginning. C will do this by translating the code into machine code through a compiler to the assembler of the first hardware and then into machine code on the second hardware.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the structure of C code?

A

Program starts with the main function, but also has a header and a body. The header specifies return type, function name and any parameters. The body is inclosed in the curly brackets and contains the code that needs to be run in the functions as well as the return statement

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does #include do?

A

include essentially includes and initialises the header file that is needed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the 5 common operations used in C?

A

+ (addition), - (subtraction), * (multiplication), / (division) and % modulo (which is division but instead of returning the division is returns the remainder of said division eg 2 % 2 = 0 but 3 % 2 = 1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is i++ and i–?

A

These are known a post-increments where it essentially increments or decremenets i after some the function has run you can also do pre-increments which is the same but just increment before the function has run and look like ++i or –i

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the common data types?

A

int, float and char (remember that C is case sensitive) as well as local (variable declared inside the function) and global (variable declared outside the function)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you print in C and what are the variable arguments for each data type?

A

to print in C yoiu can use printf, the arguments you need to include are:
1. %c for char values
2. %d for decimal
3. %f for floating points
4. %s for string

You can also use \n for newlines, \t for tabulator spaces and \a for bell/alert sound

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the difference between ‘=’ and ‘==’

A

’=’ is to assign values to an variable whereas ‘==’ is used for comparision between values

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you type cast in C?

A

If you want to type cast in C you need to add the type in front of the value of you want to change. For example char ‘A’ = A but if i do int ‘ A’ = 65 based on the ASCII values

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the comparison operators?

A

Comparision operators have a boolean result either false (0) or true (1). We can use <, >, <=, >=, == and !=

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are the logical expressions?

A

Logical expressions have a boolean result either false (0) or true (1). We can use && to represent and, || to represent or and ! to represent not

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are the bitwise logical expressions?

A

Bitwise logical expressions result in an integer result. We can use & to represent and, | to represent or, ^ to represent xor (exclusive or) and ~ to represent not. An example of this is x & y which is x and y. So when x = 1101 and y = 0101 then this just means x + y given the understanding is just 1101 + 0101 which is equal to 0101

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

C vs Assembly comparison in code

A

NEED TO REFER TO SLIDE EXAMPLES

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the structure of if-else in C?

A

if (argument in here) {
}
else{
}

You can also have an else if

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a switch statement and what is the important thing you need to remember about it?

A

switch (argument){
case 1:
break;
case 2:
break;
default:
}
So above is the structure of the switch case is a faster version of a nested if-else statement. The break statement is necessary as otherwise it will keep looping and the default is optional but it is essentially the base case

16
Q

How does a for loop work?

A

for ( int i = some number, i is < / > / <= / >=, i++/i–){
contents of loop here
}

A for loop takes a initialised variable, a terminate variable (so when the loop needs to stop and an increment variable

17
Q

How does a while loop work?

A

int i = 0
while (arguement){
contents of loop here
}
A while loop will basically say while true run loop, once the loop is not true it will end/break

18
Q

How does a do-while loop work?

A

int i = 0
do {
contents of loop here
}
while (arguement);
Similar to a while loop but the do is only done as long as the while condition is true

19
Q

What are pointers?

A

Pointers are another data structure/type, the represent the addresses in memory space and not an actual value. To get set a pointer we use *variable to get the address we use &variable. For example if i have int x = 7, meaning 7 is an integer and do return x it will give me 7. If i do &x it will return the address of x. Now if i set x as a pointer int *x and called x it would give me the address but if i called *x it would give me 7 as in this case *x is a deferencing of the value stored in the address which is 7 in this case

20
Q

What do functions do in C?

A

Functions are sub-programs that take a number of parameters and return a results IF it needs to. I say if because in the function you need to set a return type which is int, float, char, etc which then means that function needs to have a return that will return the respective value. But you can also have void, void essentially means nothing needs to be returned (no return required)

21
Q

What do arrays do in C?

A

Arrays are intialised by doing int name[size of array) for example int field[10] which means there is an array of size 10 that starts a position 0 and ends at position 9.
You can add values to array by doing name[location] = value and you can loop through arrays. REFER TO EXAMPLE ON SLIDES

22
Q

What are strings in C?

A

Strings in C are not a data type/structure but rather an array of characters. Each string much be terminated by a null character \0. Note that the null character needs and/or takes a position in the the array and thus needs to be accounted for when setting the size or adding to the array.

23
Q

EXAMPLES

A

REFER TO SLIDES FOR EXAMPLES